I have a Cocoa app for Mac OS X written in Xcode 4. The app has a main window that is the app delegate. This window has a button that opens another window (call it pop window) with 2 TextFields and a couple of buttons. When the user click one of those button the idea is to close the pop window and grab the text from the 1st TextField and use it on the app delegate.
The code I have is as follow.
App delegate .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
The Delegate .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
will pop the new window.
The the pop window code
The .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
and the .m
#import "MyClass.h"
#import "TestAppAppDelegate.h" //try to access the delegate but no luck
@implementation MyClass
@synthesize popupwin;
@synthesize one;
开发者_StackOverflow中文版@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
So the idea was that since I can't access the pop window directly I tried with having a variable made public on the app delegate and copy the value of the text field there before closing the pop window. It didn't work.
How do I do this? How do i pass the value of the text field of one window to another?
Note: No, I can't use alerts for this.
Code samples are appreciated. Thank you.
You are allocating a new instance of your application delegate. Instead of [TestApplicationDelegate alloc]
you should be using [NSApp delegate]
.
Once you have a pointer to the actual delegate, you are not using the accessor properly to set the vauleOfEdit property.
Currently you are calling initwithformat on the returned value of an accessor, which is either going to be nil or an already initialised string.
Amend your onclose
method to:
// when we hit the "Done" button
- (IBAction)onclose:(id)sender
{
TestAppAppDelegate *mainwin = (TestAppAppDelegate*)[NSApp delegate];
mainwin.valueofedit = [one stringValue];
[popupwin close];
}
@end
精彩评论