• 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: Confused about bindings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Confused about bindings


  • Subject: Re: Confused about bindings
  • From: David Aames <email@hidden>
  • Date: Sun, 19 Nov 2006 17:51:25 +0000

Thanks to both of you (mmalc & Julien). After carefully reading your replies I tried writing some code. I created a sample app to experiment with bindings and my goal is to programmatically bind two text fields to two properties in a data model class. I've created a class which is instantiated in the nib to set up the connections and which also has the text fields outlets. This class also has a "model" property which holds a reference to an object which has two properties:"name" and "password". Here's my code:

NSObjectController* controller = [[NSObjectController alloc] initWithContent:nil];
[controller bind:@"contentObject" toObject:self withKeyPath:@"model" options:nil];

DataModel* model1 = [[DataModel alloc] init];
[model1 setName:@"myname"];
[model1 setPassword:@"mypassword"];
[self setModel:model1];
[model1 release];

[tbox1 bind:@"value" toObject:controller withKeyPath:@"content.name" options:nil];
[tbox2 bind:@"value" toObject:controller withKeyPath:@"content.password" options:nil];


And this code doesn't really do anything since I can't see the values in the text fields. As a side note I also could've set the content object of the controller directly to the model object, couldn't have I? What would be the disadvantage if I went this route? Thanks.

David

PS. If the content object changes would that mean that the controller would issue a KVO message that content.name has changed (because changing the content object implies that content.name also changed) so that the view can reflect the change? Is it just me or there is no documentation on how to implement our own subclasses of NSController? Cheers.

On 19 Nov 2006, at 15:28, mmalc crawford wrote:


On Nov 19, 2006, at 6:50 AM, David Aames wrote:

But as I was reading through the docs ("View Initiated Updates", Bindings Programming Guide) it says the view has to send the new values to the controller ("Communicate view values to the controller to which it is bound") and the method call they show is:
[observedObjectForAngle setValue: newControllerAngle forKeyPath: observedPathForAngle];
But doesn't this actually bypass the controller?


No, because the observed object is the controller.

So are we actually supposed to use KVC to update the model objects ourselves (i.e. implement in on our own in our custom views)?

No, as the documentation shows you update the object to which you're bound.

If that is the case wouldn't it be better to implement the updating in the setters and not in updateForMouseEvent: as it is shown in the docs?

No, because that would potentially lead to an infinite loop when the model subsequently sends a KVO change notification.

mmalc


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Confused about bindings (From: David Aames <email@hidden>)
 >Re: Confused about bindings (From: "Julien Jalon" <email@hidden>)
 >Re: Confused about bindings (From: David Aames <email@hidden>)
 >Re: Confused about bindings (From: mmalc crawford <email@hidden>)

  • Prev by Date: Re: Confused about bindings
  • Next by Date: Launch bundled application before main application
  • Previous by thread: Re: Confused about bindings
  • Next by thread: Re: Re: Confused about bindings
  • Index(es):
    • Date
    • Thread