Re: Trying To Understand Bindings
Re: Trying To Understand Bindings
- Subject: Re: Trying To Understand Bindings
- From: Ken Thomases <email@hidden>
- Date: Sat, 13 Dec 2008 03:03:31 -0600
On Dec 13, 2008, at 12:19 AM, Bridger Maxwell wrote:
I have a custom view subclass (sineWaveView) that the user interacts
with to change a property (connectedContact). I would like to bind
this to a value (selectedShortRangeContact) in a dictionary
(database) in another object (databaseClient). Simple, right?
Anyway, I establish the binding like so:
[sineWaveView bind:@"connectedContact" toObject:databaseClient
withKeyPath:@"database.selectedShortRangeContact" options:nil];
This seems to only work one-way. When the view sets the property, it
gets set in the dictionary. However, when I update the value
directly in the dictionary, the view never gets a notification. It
is like the binding is one-way.
Hmm. You're seeing the opposite of what I would expect. There is a
default implementation of bind:toObject:withKeyPath:options:, but its
semantics are not clearly documented anywhere that I've found.
Empirically, it appears to be one-way in the opposite direction of
what you're seeing. It uses KVO to observe the key path on the object
and, when that changes, updates the property with the same name as the
binding on the receiver of the bind:... message. But changes in the
receiver's property don't automatically generate updates to the
property at the key path relative to the object. This typically
manifests as user manipulations of the view not resulting in updates
to the model.
For a custom view subclass, you really need to provide your own
implementation of bindings. See "How Do Bindings Work?" here: http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaBindings/Concepts/HowDoBindingsWork.html
If you are providing your own implementation of bindings, then you'll
have to share with us exactly what you're doing before we can help.
databaseController = [[NSObjectController alloc]
initWithContent:databaseClient];
[databaseController
bind:@"content.database.selectedShortRangeContact"
toObject:sineWaveView withKeyPath:@"connectedContact" options:nil];
Although the documentation for bind:toObject:withKeyPath:options: says
that the binding parameter is a key path, it's not. It's the name of
a binding. That _may_ correspond to a property in the KVC/KVO sense,
or it may be its own thing. (For example, NSTextView has a binding
called "value", but doesn't have a property called "value".) In any
case, it's never a path (multiple elements separated by dots), it's
just a simple name.
See here to learn about the bindings of an NSObjectController: <http://developer.apple.com/documentation/Cocoa/Reference/CocoaBindingsRef/BindingsText/NSObjectController.html
>. You are trying to set up the "content" binding of your object
controller, and you want to bind it to the databaseClient object,
using the key path "database.selectedShortRangeContact". In other
words, if you're going to interpose an object controller between your
view and coordinating controller, then the binding for the object
controller will look very much like your view binding used to look --
it's just that you send the message to the object controller and name
a binding on the controller:
[databaseController bind:@"content" toObject:databaseClient
withKeyPath:@"database.selectedShortRangeContact" options:nil];
and then you'd bind your view to the object controller:
[sineWaveView bind:@"connectedContact" toObject:databaseController
withKeyPath:@"content" options:nil];
Again, though, for a custom view subclass you'll have to implement
what exactly such a binding means. Bindings for your own classes
don't come for free.
So, I set it up correctly and I don't get that error. However, even
when it is set up correctly, I get the following error when I the
view changes the value:
[<NSObjectController 0x1062fa0> setValue:forUndefinedKey:]: this
class is not key value coding-compliant for the key
content.database.selectedShortRangeContact.
The above is a hint about using a key path where a key is expected.
Note that it's complaining about a key, but when it prints out the
name of the key it's trying to work with, it's a key path.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden