2008 06 05One way binding to NSSlider
I was trying to bind an
NSSlider
's value to another object. I figured it would be nice to set the slider's value in IB and have it act as default for the bound object. While binding the slider to the object ([myObject bind:@"opacity" toObject:opacitySlider withKeyPath:@"value" options:nil];
), my code kept crashing with
*** Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[<NSSlider 0x125040> valueForUndefinedKey:]: this class is not key value coding-compliant for the key value.'
What ? NSSlider
does not have a value key ? That's new ! It's there in IB, it shows up in its exposedBindings … Turns out only one way is supported : binding the view (slider) to the object.
BAD [myObject bind:@"opacity" toObject:opacitySlider withKeyPath:@"value" options:nil];
GOOD [opacitySlider bind:@"value" toObject: myObject withKeyPath:@"opacity" options:nil];
NSTextField
. [slider valueForKey:@"value"]
will crash, too. I suppose all the controls act alike, that means there's no KVC way to interact with them.
Patrick Geiller
2008 06 06
Any idea why it's 'faked' and just one way ? It seems like more work than just making a 'value' property KVC compliant.
Yes there is, you use the accessors defined in NSControl (
objectValue
,doubleValue
,floatValue
,intValue
,stringValue
).Binding to something requires a KVO-compliant key. Binding from something can evidently be faked (or at least renamed).