NSUserDefaults not working when opening a nib programmatically
In the window which shows up at launch and NSUserDefaults works great with the initial window. If i open the same nib file again programmatically with this code:
NSWindowController *controller = [[NSWindowController alloc] initWithWindowNibName:@"MainMenu"];
[controller showWindow:self];
it fails to get the NSUserDefaults. I get the value of (null)
. I read the defaults like this:
[[NSUserDefaults standardUserDefaults] valueForKe开发者_开发知识库y:@"myKey"];
and write to them like this:
[[NSUserDefaults standardUserDefaults] setValue:@"myValue" forKey:@"myKey"];
What am I doing wrong? Once the value is set and I relaunch the application, reopen the prog. loaded nib window it fails!
I open this programmatically opened window from the class which the NIB file is hooked up to -> The user presses a button and the 2nd window opens!
I found the problem myself now. I had a NSUserDefaults variable defined in the .h so i can access it on the whole .m file. I was initializing it, mainDefaults = [NSUserDefaults standardUserDefaults]
, in the - (void)applicationDidFinishLaunching:(NSNotification *)aNotification{}
, which was only called when the application started! When I opened a new window, that wasn't called of course!
Anyway, thanks for your help.
精彩评论