Re: Bindings on custom objects
Re: Bindings on custom objects
- Subject: Re: Bindings on custom objects
- From: Yann Bizeul <email@hidden>
- Date: Wed, 30 May 2007 01:19:39 +0200
Hi,
If facts bindings are not fully working as many expects out of the box.
That means if you have a view A and a controller or object B, you can
bind your view to your object, but you implement your view so that it
will notify changes yourself.
This thread has quite good informations on the subject : http://
www.cocoabuilder.com/archive/message/cocoa/2007/4/30/182673
As far as I understand bindings right now :
when you [objA bind:"akey" toObject:objB withKeyPath:aKeyPath
options:nil], here is what happens :
objA registered itself as an observer for changes in objB for key
path aKeyPath, this is provided out of the box by Apple (at least
10.4.9 :-) that's why your binding seem to work in one way) but,
When something changes in objA, it must notify the objects it is
observing and call KVC methods for the key that changed, and this is
NOT provided as a default, you have to implement it yourself. I did
it with a simple category of NSObject, so that when you change
something in objA (and only if something really change) you call
[self notifyBinding:aKeyPath].
The 30s category I wrote could be better I guess :
- (void)notifyBinding:(NSString*)aBinding;
{
NSDictionary *info = [self infoForBinding:aBinding];
if (! info)
return;
id observedObject = [info valueForKey:NSObservedObjectKey];
NSString *keyPath = [info valueForKey:NSObservedKeyPathKey];
[observedObject setValue:[self valueForKey:aBinding]
forKeyPath:keyPath];
}
I hope that helps
Yann Bizeul - yann at tynsoe.org
Cocoa Developer
Tynsoe Projects
BuddyPop - GeekTool - SSH Tunnel Manager - ...
http://projects.tynsoe.org/
Le 30 mai 07 à 00:21, Daniel Angermeier a écrit :
Hi,
I've bound object A to object B programmatically.
Both inherit directly from NSObject (they are non view or
controller classes).
and both are KVC compliant for the given properties.
(self = objectA)
[self bind:@"currentValue" toObject:objectB withKeyPath:aKeyPath
options:nil];
However the binding only works in one direction, that is
if I change the key path of object B, object A updates its bound
property correctly,
however if I change the given key on object A, the change is not
propagated to
object B.
Now, if I change the order of the binding, I am getting an undefined
key exception:
[objectB bind:aKeyPath toObject:self withKeyPath:@"currentValue"
options:nil];
can the bind:toObject:withKeyPath:options method only be invoked on
NSView (and subclasses) objects ?
Could somebody please tell me what I am obviously doing wrong in the
code above ?
Any help would be greatly appreciated.
Many thanks in advance!
Daniel
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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