cemulate
cemulate

Reputation: 2333

Cocoa bindings: custom setter methods?

I'm using Cocoa bindings to manage a table of objects. I understand how bindings work but I've run into a slight problem. Managing the table of objects would be fine and dandy, except that those objects have to manage actual bluetooth hardware. I'm working off of a framework that provides a class representing a connection to this hardware, and have made another "manager" class the makes it key-value compliant. In other words, this manager class has to be able to connect and modify its "connect" status in its properties dictionary, be the delegate of this hardware and modify properties, and update the hardware with changes made.

However, whenever I set new values within the object itself, like in a "connect" method that would change the "connect" key's value to 2 (looking), (i.e. propertiesDict = newDict), the change is not seeming to be picked up by observers that it is bound to. I've looked at the observeValueForKeyPath:ofObject:change:context: in the NSKeyValueObservingProtocol. However, I don't know what to do with the context argument.

I hope that makes sense... but if anyone has any ideas I'd love to hear them.

Upvotes: 1

Views: 1045

Answers (2)

Bruno Berisso
Bruno Berisso

Reputation: 1091

There are three ways to update a property/attribute in a KVO compatible way:

  1. Using the property setter (specified in @property declaration or generated by @synthesize)
  2. Calling -willChangeValueForKey: and -didChangeValueForKey: before and after you change the property value in any way.
  3. Calling -setValueForKey:

Upvotes: 0

Marc Charbonneau
Marc Charbonneau

Reputation: 40517

Your question isn't totally clear, but if I'm understanding it correctly the issue might be because you need to send manual KVO notifications before and after you change a value in the embedded object. For instance, [self willChangeValueForKey:@"connected"]; and [self didChangeValueForKey:@"connected"];.

Upvotes: 2

Related Questions