Re: Bidirectional, Manual Binding in custom control
Re: Bidirectional, Manual Binding in custom control
- Subject: Re: Bidirectional, Manual Binding in custom control
- From: Quincey Morris <email@hidden>
- Date: Wed, 27 Jun 2012 22:42:15 -0700
On Jun 27, 2012, at 22:12 , Jerry Krinock wrote:
>> On May 21, 2012, at 20:44 , Jerry Krinock wrote:
>
>>> -(void)setRating:(float)rating
>>> {
>>> // Stuff to make reverse binding work…
>>> NSDictionary* bindingsInfo = [self infoForBinding:@"rating"] ;
>>> id object = [bindingsInfo objectForKey:NSObservedObjectKey] ;
>>> NSString* bindingsPath = [bindingsInfo objectForKey:NSObservedKeyPathKey] ;
>>> [object setValue:[NSNumber numberWithFloat:rating]
>>> forKeyPath:bindingsPath] ;
>>>
>>> // Set ivar, needsDisplay
>>> …
>>> }
>
> But after I bound instead to a "multiFoo" class to make that work, I found that the extra setting of the data model was causing model values to be copied from one model object to the next when the I simply selected one model object and then changed the selection to a different model object.
Interesting stuff. :)
Looking back at this code, it seems that you probably always needed to separate the two "directions" of value-changing. Since the model->view updates are coming via KVO, the setter should only contain the second part ("Set ivar, needsDisplay").
It seems to me this should prevent selection changes from rebounding onto the data model.
> A better place for pushing the value to the data model is in the input-handling method of the view, -mouseDown: in the case of my star rating control. Moving the above code to -mouseDown: fixed the problem.
Using mouseDown kinda feels wrong to me. Wouldn't it make more sense to do the first part of the existing code ("Stuff to make reverse binding work") in an action method?
> - (IBAction) changeRating: (sender)
> {
> // Stuff to make reverse binding work…
> NSDictionary* bindingsInfo = [self infoForBinding:@"rating"] ;
> id object = [bindingsInfo objectForKey:NSObservedObjectKey] ;
> NSString* bindingsPath = [bindingsInfo objectForKey:NSObservedKeyPathKey] ;
> [object setValue:[sender objectValue] // since the ivar doesn't have the value yet
> forKeyPath:bindingsPath] ;
> // this value should now bounce back to the control via the setter, thus
> }
That way you're not dependent on *how* the control gets a new value.
_______________________________________________
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