I have a custom class (subclass of NSView – actually let’s say a modified editor, but NOT a subclass of NSTextView) which I’m binding to an NSArrayController programmatically (I most definitely canNOT do it via Interface Builder), like this :
[myEditor bind:@"string"
toObject:myController
withKeyPath:@"selection.content"
options:nil];
The above works, however when the value is changed, it is NOT updated to my NSArrayController – it’s as if it doesn’t “stick”.
I’ve even tried, using the options below, but to no avail :
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],NSContinuouslyUpdatesValueBindingOption,
[NSNumber numberWithBool:YES],NSAllowsEditingMultipleValuesSelectionBindingOption,
[NSNumber numberWithBool:YES],NSConditionallySetsEditableBindingOption,
[NSNumber numberWithBool:YES],NSRaisesForNotApplicableKeysBindingOption,
nil];
Any ideas?
A class has to implement support for bindings. The view and cell classes that come with Cocoa generally implement a particular set of bindings. Whether custom subclasses do or not is up to the implementer.
If a class hasn’t implemented specific support for a binding, then a
-bind:...request falls through toNSObject‘s implementation. However, that implementation is quite limited. It observes the key path for the observableController, and updates via KVC the property of the receiver having the same name as the binding. But it does not go in the other direction. That is, changes to the property on the receiver are not forwarded to the observableController via the key path.To make this less abstract using your example. If the class of
myEditorhas not specifically implemented support for a “string” binding, thenNSObject‘s implementation will do[myController addObserver:<some private observer object> forKeyPath:@"selection.content" options:<...> context:<...>].When the private observer object receives a change notification, it will do
[myEditor setValue:[myController valueForKeyPath:@"selection.content"] forKey:@"string"].However,
NSObjectwill make no attempt to observemyEditor‘s “string” property nor ever call[myController setValue:<...> forKeyPath:@"selection.content"].To learn more about how to implement support for a binding, see Apple’s documentation.