0

I'm trying to bind a property from a custom NSView to another on a custom NSObject. Both properties are simple bool value.

As a newbie with custom binding i've read Apple Documentation and searched on stackoverflow.

So I created a custom NSView and a custom NSObject, added a bool property called 'enabled' to both and bind them with [myCustomView bind:@"enabled" toObject:myObject withKeyPath:@"enabled" options:nil];

My customView use the approach explained in this article http://www.tomdalling.com/blog/cocoa/implementing-your-own-cocoa-bindings/ to notify value's changes and prevent memory retain issues.

I started my app, it works ! Wonderful ... but it's only one way binding ! When I click my customView, the custom object's property is updated (right) but if the custom object's property value change, my custom view's property isn't updated (Grrrr)

I'm a bit confused because as far as i understood custom bindings, Apple recommendation is to manually implement bind:toObject:withKeyPath:options and register an observer to track property's value changes and Tom Dalling's approach says the opposite.

So what is the best way to bind my properties in a bi-directional way ?

miosepayo
  • 1
  • 1
  • "I've heard of two ways of doing something. I tried one of them and it didn't work. Now I'm all out of ideas!" – Ken Thomases May 07 '16 at 20:07
  • Do you change the value of your custom object's property in a KVO compliant way? – Willeke May 08 '16 at 01:15
  • What do you mean by change the value of property in a kvo compliant way ? By registering an observer ? – miosepayo May 08 '16 at 06:29
  • How do you change the value of your custom object's property? – Willeke May 08 '16 at 20:57
  • I simply assign a new value for my customObject's property. `customObject.enabled = value` I also tried this syntax `[customObject setValue:[NSNumber numberWithBool:value] forKey:@"enabled"];` but it doesn't work. – miosepayo May 09 '16 at 06:54

2 Answers2

0

This SO answer here indicates that you just setup a symmetric binding going the other way. I haven't tried this, but it sounds like the following will work.

[myObject bind:@"enabled" toObject:myCustomView withKeyPath:@"enabled" options:nil];
Community
  • 1
  • 1
0

I'm a bit confused because as far as i understood custom bindings, Apple recommendation is to manually implement bind:toObject:withKeyPath:options and register an observer to track property's value changes and Tom Dalling's approach says the opposite.

You have to implement both. You have to observe myObject with keypath @"enabled" so when myObject.enabled changes you can change myCustomView.enabled. You have to set myObject.enabled when myCustomView.enabled changes.

See Responding to Changes

Willeke
  • 14,578
  • 4
  • 19
  • 47