Question

I have been searching everywhere for this and not been able to find it and its really bugging me. I can't figure out how to save the current state of an NSPopUpButton, so if a user changes it, the app will load that state next time they run the app. I have tried NSUserDefaults and its just been adding a new item to the pop up button that has a really weird name.

Was it helpful?

Solution

It sounds like you've failed to set an initial value for the user default.

For example, if you bind your NSPopupButton using the "Selected Value" binding to a default "popupSelection" then you must insure that NSUserDefaults always returns one of the NSPopupButton's entries for "popupSelection".

You achieve this by including a plist in your project, say "Defaults.plist", which contains the default value you wish for "popupSelection" (and the default values you wish for your other preferences), then at application startup load this plist from your bundle:

NSString *defaultsPath = [[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"];
NSDictionary *defaultsDict = [NSDictionary dictionaryWithContentsOfFile:defaultsPath]];

and register these as your defaults with NSUserDefaults:

[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict];

Now your popup button will start with a correct value, and the binding will keep the value in NSUserDefaults correct when the user changes the selection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top