2011-09-26 188 views
0

我有一个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 

这样的想法是,既然我不能直接访问弹出窗口我试着让应用程序代理公开一个变量,然后在关闭弹出窗口之前复制文本字段的值。它没有工作。

我该怎么做?我如何将一个窗口的文本字段的值传递给另一个窗口?

注意:不,我无法为此使用警报。

代码示例表示赞赏。谢谢。

回答

1

您正在分配应用程序委托的新实例。而不是[TestApplicationDelegate alloc]你应该使用[NSApp delegate]

一旦你有一个指向实际委托的指针,你没有正确使用该访问器来设置vauleOfEdit属性。

目前,您正在调用initwithformat函数来获取访问器的返回值,该访问器要么是零,要么是已经初始化的字符串。

修改你onclose方法:

// when we hit the "Done" button 
- (IBAction)onclose:(id)sender 
{  
    TestAppAppDelegate *mainwin = (TestAppAppDelegate*)[NSApp delegate]; 

    mainwin.valueofedit = [one stringValue]; 
    [popupwin close];  
} 
@end 
+0

在哪里?怎么样?如果我在弹出窗口中替换代码,我得到'***初始化方法-initWithFormat:locale:arguments:不能被发送到类NSCFString的抽象对象:创建一个具体实例!' –

+0

替换的代码在'onclose'上在弹出窗口中:'TestAppAppDelegate * mainwin = [NSApp delegate];' –

+0

谢谢,我现在很困惑。你可以发布一整块代码吗? 'self.valueofedit'是什么?从弹出窗口(MyClass)或主窗口(TestApplicationDelegate)? –

相关问题