I'm still new to Cocoa Bindings and I haven't found an answer to this question even after looking through the docs. What I want to do is have a segmented control that only has two segments. If the first segment is selected, then a preference in NSUserDefaults should be YES , but if the second segment is selected, then the preference should be NO. This is trivial to do through code:
-(IBAction)segmentSelectionChanged:(id)sender {
NSInteger selectedSegment = [sender selectedSegment];
[[NSUserDefaults standardUserDefaults] setBool:(selectedSegment==0)?YES:NO forKey:@"somepref"];
}
but I'd like to do it through bindings (selected index looks promising). Any way to do something like this? Thanks!
I think you've got it already -- binding the control's selectedIndex
in IB:
Bind To: Shared User Defaults Controller Controller Key: values Key Path: WhateverYouWant
seems to work just fine.
Is the problem that you really need it to be a BOOL
? It's just a typedef
for signed char
anyways. See objc.h, lines 43, 49, and 50:
typedef signed char BOOL;
// ...
#define YES (BOOL)1
#define NO (BOOL)0
You can pull the value back out using integerForKey:
and cast it (possibly better because more explicit):
(BOOL)[[NSUserDefaults sharedUserDefaults] integerForKey:@"WhateverYouWant"];
or just continue using boolForKey:
and it should work fine.