On May 19, 2010, at 2:05 AM, Mazen M. Abdel-Rahman wrote:

> I have a custom view class with an array property.  I want to bind that array 
> property to a model - which in my case is an array that is in a 
> container/manager object.  The binding is done via  a custom view controller 
> (in it's awakeFromNib). (calendar is the model object)
> 
>       [calendarView bind:@"events" 
>                         toObject:calendar 
>                  withKeyPath:@"events" 
>                          options:options];
> 
> Now whenever "events" in the object calendar is modified "events" in 
> calendarView is modified as well. 
> 
> In addition - observeValueForKeyPath... is getting called as well in the 
> calendarView.
> 
> The problem is that if some items are removed from calendar's (the model) 
> "events array" by the time observeValueForKeyPath in calendarView  gets 
> called its "events" array has already been updated to reflect the model 
> change.  

I think you have two options:

* Override -bind:toObject:withKeyPath:options: and don't rely on the default 
implementation, at all.

* Use the default implementation but don't use 
-observeValueForKeyPath:ofObject:change:context: to be informed about the 
change of the view's property.  Use the property's setter method, instead.


I would not expect that the view's property would already be updated at the 
time that -observeValueForKeyPath:... is called, because by what mechanism 
other than that method would the binding update that property?  The answer, I 
guess, is that the default implementation of -bind:... is actually setting some 
other object (not the receiver) as the key-value observer of the key path of 
the observableController, and that object is getting the 
-observeValueForKeyPath:... message and then setting the property on your view. 
 However, if that's the case, I don't know why your view would have 
-observeValueForKeyPath:... invoked on it, too.  So, there's a bit of a mystery 
there.  And that's reason enough not to rely on the default implementation -- 
it's a black box with ill-defined behavior.

Overriding -bind:... is actually the method illustrated in Apple's 
documentation about implementing a custom binding on a custom view.  See the 
"How Do Bindings Work?" section of the Cocoa Bindings Programming Topics.

Also, have a look at the Sketch+Accessibility sample code, where it implements 
custom binding (e.g. SKTGraphicView).  It actually illustrates both approaches.

Regards,
Ken

_______________________________________________

Cocoa-dev mailing list ([email protected])

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