0

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?

Developer
  • 406
  • 1
  • 4
  • 18

1 Answers1

1

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?

  • 3. Are you sure loadText is executed? – Willeke Sep 03 '15 at 16:04
  • Yes, I created an IBAction. When you click the button the method will be executed. I made a mistake when I asked this question. I'll correct my mistake. – Developer Sep 03 '15 at 19:05
  • 1. How can you see if the method is nil? – Developer Sep 03 '15 at 19:06
  • Your app can run multiple threads, even if you don't expressly create new threads. All actions on the UI must be called while the main thread is running. But if your method is called as the result of an IBAction then you should be running on the main thread. Set a breakpoint in your loadText method to make sure your textField is valid and not nil. – Eric Crichlow Sep 04 '15 at 03:32