Remembering the Selection of a NSPopUpButton for the next launch
I am trying to get my app to remember the selection of an NSPopUpButton for the next time the App is launched. I tried binding the Selection Index开发者_如何学Python to a NSUserDefaultsController but it has no effect, It doesn't remember the selection for the next launch. What do I need to do?
Binding the selected index to the shared NSUserDefaultsController
works for me. You need to ensure you set the Controller Key to values
and the Model Key Path to a unique preferences key string, such as widgetPopupSelectionIndex
.
Also, make sure that you are not exiting your app by clicking the Stop button in Xcode, because defaults are synchronized during the application termination process and if you just kill the app from Xcode the app doesn't get the opportunity to persist the user defaults.
This is a problem that often bites me when I'm working on user defaults-related code. If you quit your app from the application menu in the app then the user defaults should be stored correctly.
I haven't played around with bindings too much, so I can't help you figure out what's going wrong there. I would solve this by saving the -[NSPopUpButton indexOfSelectedItem]
to [NSUserDefaults standardUserDefaults]
at termination and restoring it at launch.
精彩评论