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