Re: Cocoa bindings one- or bi-directional
Re: Cocoa bindings one- or bi-directional
- Subject: Re: Cocoa bindings one- or bi-directional
- From: mmalcolm crawford <email@hidden>
- Date: Thu, 26 May 2005 19:39:30 -0700
On May 26, 2005, at 11:51 AM, Kay Roepke wrote:
Here is a simple example with to model objects bound to each other:
BinderBrick *brick1 = [[BinderBrick alloc] init];
BinderBrick *brick2 = [[BinderBrick alloc] init];
[brick1 bind:@"intValue" toObject:brick2
withKeyPath:@"intValue" options:nil];
[brick1 setIntValue:5];
NSLog(@"%d, %d", brick1->_intValue, brick2->_intValue);
[brick2 setIntValue:7];
NSLog(@"%d, %d", brick1->_intValue, brick2->_intValue);
The output is:
2005-05-26 16:08:12.436 Binder[1357] 5, 0
2005-05-26 16:08:12.437 Binder[1357] 7, 7
This is correct behavior. You tell brick1 to observe changes on the
keypath intValue of the object brick2.
This is at best misleading. The code specifies binding, not
observing. The basic goal of binding is to keep two values
synchronised. If you use the bind:... method to bind a text field's
'value' to a keypath in an object controller, you could get the
expected behaviour.
brick2 doesn't know anything about brick1
brick2 knows enough about brick1 to send KVO notifications when
intValue changes...
- that wouldn't be really helpful, would it?
... it would be helpful if the two were synchronised as expected ...
The issue here is that binding is "one-way" if you only use the
behaviour you get for free. You can bind a KVC-compliant attribute
of any object ("object A") to the property of another object ("object
B"), and object A will register as an observer of the property of
object B and update its (object A's) attribute in response to changes
in the observed property value. What it won't do, however, is to
push changes the other way -- that is, by default you have in effect
a "read-only" binding. If object A's attribute value is changed,
object B is not updated. To get this behaviour, you must write
custom code -- see:
<http://developer.apple.com/documentation/Cocoa/Conceptual/
CocoaBindings/Concepts/HowDoBindingsWork.html>
If you want it the other way round, you'll need to tell brick2 what
to observe, too.
This may actually be more appropriate here. If you set both objects
to observe each other, then they can keep themselves synchronised...
mmalc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden