将TextField的stringValue从一个类传递到另一个类

时间:2011-09-26 18:22:06

标签: objective-c macos cocoa

我有一个用Xcode 4编写的Mac OS X的Cocoa应用程序。该应用程序有一个主窗口,即应用程序委托。这个窗口有一个按钮,打开另一个窗口(称之为弹出窗口),带有2个TextFields和几个按钮。当用户单击其中一个按钮时,想法是关闭弹出窗口并从第一个TextField中获取文本并在app delegate上使用它。

我的代码如下。

App代表.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

所以我的想法是,由于我无法直接访问弹出窗口,我尝试在app委托上公开变量,并在关闭弹出窗口之前复制文本字段的值。它不起作用。

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

注意:不,我不能使用警报。

感谢代码示例。谢谢。

1 个答案:

答案 0 :(得分:1)

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

一旦有了指向实际委托的指针,就不能正确使用访问器来设置vauleOfEdit属性。

目前,您正在调用访问器的返回值的initwithformat,该访问器将是nil或已经初始化的字符串。

将您的onclose方法修改为:

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

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