How to save the state of an NSPopUpButton? - Cocoa

778 views Asked by At

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.

1

There are 1 answers

1
CRD On BEST ANSWER

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.