NSUserDefaults, One Preference with three options
Thanks for the help.
I'm having a hard time with a preference that has three options by way of checkboxes. More specifically, 开发者_如何学编程the relationship between the bool states of three checkboxes that control the preference. I comfortable setting up single checkbox preferences, and the individual preference settings in this three option version all work. However I can't seem to get the checkboxes to interact consistently. So one on, two off, etc.
This is my code. What's the best way to get this done?
Thanks.
Paul
- (BOOL)displayBankOne
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
return [defaults boolForKey:PREFdisplayBankOne];
}
- (IBAction)bankOne:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:displayBankOne forKey:PREFdisplayBankOne];
[defaults setBool:displayBankOne forKey:PREFdisplayBankOne];
[defaults setBool:displayBankTwo forKey:PREFdisplayBankTwo];
[defaults setBool:displayBankThree forKey:PREFdisplayBankThree];
[bankOneFlag setState:NSOnState];
[bankTwoFlag setState:NSOffState];
[bankThreeFlag setState:NSOffState];
}
If I understand you right, you could try using a Radio Group (NSMatrix). Instead of 3 individual check boxes. And use the binding in IB to bind their Value Selection to the shared User Defaults.
In binding in IB, (Matrix bindings) Just tick bind to: shared User Defaults Controller. And give the pref entry a name in the Model Key Path.
To give you an idea:i You can even bind other Objects to the same Model Key Path name. i.e you can add a Label (NSTextField) in IB, and bind its value to the shared User Defaults Controller. And set its Value Model Key Path to the same name you gave the name in the Model Key Path for the Radio Group.
When run, the Label will display the index number of which radio button is on.
精彩评论