Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: How to bind a boolean property to NSCellStateValue?

I would like to bind the boolean enabled property of an NSTextField to the state of an NSButton. I already tried adding a custom NSValueTransformer that transforms the state of the NSButton into NSNumber. However, in that scenario the text fields are disabled all the time for some reason. My second approach: To bad fails also since NSValueTransformer does not offer return primitives types such as BOOL.

Example:
The screenshot shows an example in which the text fields are disabled because the checkbox has the state NSOnState. I also would like to bind the labels to this state.

Disabled text fields

Further, it would be convenient, if I could set a "disabled text" in Interface Builder. In the above example I set the text in the associated class.


Edit:

I set self.anonymousLoginCheckbox.state as the Model Key Path for the enabled property of the account text field. Similar for the password text field. However, it does not work.

Interface Builder Binding


Update:

I created an example project available on GitHub showing the implementation kindly described by Nicolas Bachschmidt.

like image 207
JJD Avatar asked Jun 25 '12 17:06

JJD


1 Answers

NSButton isn't KVO compliant for the key state. Cocoa Bindings require the observed object to emit notifications when the observed property changes. As NSButton's state is just a wrapper for its cell's state, -[NSButton setState:] method (and the automatic KVO notifications) isn't invoked when the user click the button (but -[NSCell setState:] is). If you set the model key path to self.anonymousLoginCheckbox.cell.state, it will work.

like image 104
Nicolas Bachschmidt Avatar answered Sep 30 '22 09:09

Nicolas Bachschmidt