我有一個Cocoa應用程序,用於在Xcode 4中編寫的Mac OS X.該應用程序具有一個主窗口,即應用程序委託。這個窗口有一個按鈕,用2個TextFields和幾個按鈕打開另一個窗口(稱爲彈出窗口)。當用戶單擊其中一個按鈕時,想法是關閉彈出窗口並從第1個TextField中獲取文本並在應用程序委託上使用它。將TextField的stringValue從一個類傳遞到另一個類
我的代碼如下。
應用程序的委託.H:
@interface TestAppAppDelegate : NSObject <NSApplicationDelegate> {
NSString *valueofedit;
@private
NSWindow *window;
NSPersistentStoreCoordinator *__persistentStoreCoordinator;
NSManagedObjectModel *__managedObjectModel;
NSManagedObjectContext *__managedObjectContext;
NSTextField *_StatusLabel;
}
@property (assign) IBOutlet NSWindow *window;
@property (nonatomic, retain) NSString *valueofedit;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (assign) IBOutlet NSTextField *StatusLabel;
- (IBAction)GetStatClick:(id)sender;
- (IBAction)OnLaunch:(id)sender;
- (IBAction)saveAction:sender;
@end
的代表.M:
#import "TestAppAppDelegate.h"
#import "MyClass.h"
@implementation TestAppAppDelegate
@synthesize StatusLabel = _StatusLabel;
@synthesize valueofedit;
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
valueofedit = [[[NSString alloc] init] autorelease];
}
- (IBAction)GetStatClick:(id)sender {
// I need to get the value of the pop window textfield here.
}
- (IBAction)OnLaunch:(id)sender {
MyClass *controllerWindow = [[MyClass alloc] initWithWindowNibName:@"pop"];
[controllerWindow showWindow:self];
// this of course is always null
NSString * tmp = [controllerWindow valueofedit];
NSLog(@"result: %@", tmp);
}
@end
OnLaunch
會彈出新的窗口。
的彈出窗口代碼
的.H:
@interface MyClass : NSWindowController {
NSString *valueofedit;
@public
NSTextField *one;
NSTextField *two;
NSWindow *popupwin;
}
@property (assign) IBOutlet NSWindow *popupwin;
@property (assign) IBOutlet NSTextField *one;
@property (assign) IBOutlet NSTextField *two;
@property (nonatomic, retain) NSString *valueofedit;
- (IBAction)onclose:(id)sender;
@end
與.m
#import "MyClass.h"
#import "TestAppAppDelegate.h" //try to access the delegate but no luck
@implementation MyClass
@synthesize popupwin;
@synthesize one;
@synthesize two;
@synthesize valueofedit;
// when we hit the "Done" button
- (IBAction)onclose:(id)sender
{
// the value of the textfield that I need
valueofedit = [one stringValue];
// I tried to get the value sent to the app delegate
TestAppAppDelegate *mainwin = [TestAppAppDelegate alloc];
[[mainwin valueofedit] initWithFormat:@"%@", valueofedit];
[popupwin close];
}
@end
這樣的想法是,既然我不能直接訪問彈出窗口我試着讓應用程序代理公開一個變量,然後在關閉彈出窗口之前複製文本字段的值。它沒有工作。
我該怎麼做?我如何將一個窗口的文本字段的值傳遞給另一個窗口?
注意:不,我無法爲此使用警報。
代碼示例表示讚賞。謝謝。
在哪裏?怎麼樣?如果我在彈出窗口中替換代碼,我得到'***初始化方法-initWithFormat:locale:arguments:不能被髮送到類NSCFString的抽象對象:創建一個具體實例!' –
替換的代碼在'onclose'上在彈出窗口中:'TestAppAppDelegate * mainwin = [NSApp delegate];' –
謝謝,我現在很困惑。你可以發佈一整塊代碼嗎? 'self.valueofedit'是什麼?從彈出窗口(MyClass)或主窗口(TestApplicationDelegate)? –