Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa binding programmatically + not updating value?

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?

like image 951
Dr.Kameleon Avatar asked Jun 28 '26 00:06

Dr.Kameleon


1 Answers

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 to NSObject'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 myEditor has not specifically implemented support for a "string" binding, then NSObject'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, NSObject will make no attempt to observe myEditor'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.

like image 113
Ken Thomases Avatar answered Jun 29 '26 13:06

Ken Thomases