Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Programmatic Binding KVC KVO



On Wed, Nov 4, 2009 at 12:17 PM, Richard Somers
<email@hidden> wrote:
> All code in the custom view that touch the num property use the accessor
> methods. The controller in the nib is in entity mode and bound to File's
> Owner (MyDocument) managed object context.

Okay, but as I said before, the default NSObject bindings
implementation only set up KVO in one direction.  If you call -[myView
bind:@"num" toObject:myController withKeyPath:@"someModelKeyPath"
options:0], KVO will only occur from [myController].someModelKeyPath
--> [myView].num.  NSObject doesn't (and can't) also set up the
reverse direction.

> @interface MyView : NSView
> {
>     double num;
> }
> @end
>
> @implementation MyView
>
> - (double)num
> {
>     return num;
> }
>
> - (void)setnum:(double)newNum

This is not a KVC-compliant accessor for the num property.  It needs
to be named -setNum:.

> {
>     [self willChangeValueForKey:@"num"];

Do not do this if you have not overridden
-automaticallyNotifiesObserversForKey: to return NO for the num key.

>     [myView bind:@"num" toObject:controller withKeyPath:@"selection.num"
> options:nil];

This invokes the default implementation of
-bind:toObject:withKeyPath:options:, which takes care of the model ->
view communication.  You now need to take care of the view -> model
communication.  As described in the User Updates a Value in the User
Interface section of the Cocoa Bindings Programming Topics, you can
call -setValue:forKeyPath: on the toObject and withKeyPath arguments
of the original binding (you can get this information by calling
-infoForBinding).  Or you might have some custom logic that informs
the controller/model of the change in a different way.

--Kyle Sluder
_______________________________________________

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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden

References: 
 >Programmatic Binding KVC KVO (From: Richard Somers <email@hidden>)
 >Re: Programmatic Binding KVC KVO (From: Kyle Sluder <email@hidden>)
 >Re: Programmatic Binding KVC KVO (From: Richard Somers <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.