I'm working on an Mac OS X app that'll save and load data (sort of notes app). I've added some NSTextFields and I want to change its string value when the load button is pressed. I want to do that in a separate class. I created the class and called it Load. Now I have the Load.h and the Load.m file. I made an IBAction for when the button is pressed:
- (IBAction)loadText:(id)sender
{
[textField setStringValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"MyNote"]];
NSLog(@"This method works!");
}
Xcode (7 beta 5) doesn't see any errors. But when I run it it doesn't do anything with the textfield, but it does log the string. When I was trying to find a way to edit the string value I found if you create an outlet for the NSTextField in the AppDelegate it'll work. In app Delegate:
- (IBAction)loadTextInAppDelegate:(id)sender
{
[appDelegateTextField setStringValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"MyNote"]];
NSLog(@"This method works!");
}
It's good to know there's a possibility to do it that way, but then my AppDelegate.m file will be a mess because I'll have to put in all the things that I wanted to put in the Load.m file.
So is there a way to change the string value of the NSTextField in a different class than the AppDelegate?
Two things.
1: are you sure textField in your loadText method isn't nil?
2: are you sure this method is executing on the UI (main) thread?