On Sep 24, 2008, at 4:32 PM, D.K. Johnston wrote:


- (IBAction)buttonClick:(id)sender;
@end
Both properties are synthesized in the implementation files. Here's what buttonClick: does:
        [self.model changeMyInteger];

This method is not KVO compliant (<http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/KVOCompliance.html >). I'd guess that the implementation of changeMyInteger is not KVO- compliant for 'integer' either.


A MyController instance is in the nib file, and it creates an instance of MyModel when it awakes. I have a view in the nib with an NSTextField, which is bound to MyController with the key path model.myInteger. I start it up, and the initial value of myInteger (set by MyModel) is in the textfield. But when I click the button, nothing happens: the textfield stays the same.

Because you're not invoking a KVO-compliant method.


I get buttonClick: to do this instead:

        [self willChangeValueForKey:@"model"]; // Thanks Jason!
        [model changeMyInteger];
        [self didChangeValueForKey:@"model"];

Don't do this. You should make sure you're invoking KVO-compliant methods, not sprinkling KVO change notification method invocations throughout your code.

and it works just as it should: the value in the text field changes when I click the button. So now I'm trying to understand why the message to self.model won't work. Doesn't that form do the KVO notification?

No.  Why would it?  It's not changing a value.

This "works" because you're telling the object controller that the whole model object has changed, so everything "downstream" from it is recalculated.

I suppose I could just surround everything with willChange... and didChange...;

Don't do that.

but I would like to understand what's going on.

It's all documented in <http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/ >.

Unless you've opted out of automatic change notification (<http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/Concepts/AutoVsManual.html >), you should simply invoke the relevant set accessor, either directly from your controller or within the body of your changeInteger method:

- (IBAction)buttonClick:(id)sender {
    NSInteger currentValue = model.myInteger;
    model.myInteger = currentValue + 1;
    // exactly the same as [model setMyInteger:(currentValue + 1)];
}

or

- (void)changeMyInteger {
    self.myInteger = myInteger + 1;
    // exactly the same as [self setMyInteger:(myInteger + 1)];

}


mmalc

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to