• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Properties and bindings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Properties and bindings


  • Subject: Re: Properties and bindings
  • From: mmalc crawford <email@hidden>
  • Date: Fri, 26 Sep 2008 07:43:04 -0700


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 (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


References: 
 >Properties and bindings (From: "D.K. Johnston" <email@hidden>)
 >Re: Properties and bindings (From: "D.K. Johnston" <email@hidden>)

  • Prev by Date: Fastest color detection in image?
  • Next by Date: Re: Fastest color detection in image?
  • Previous by thread: Re: Properties and bindings
  • Next by thread: NSURLDownload resumeData always nil
  • Index(es):
    • Date
    • Thread