问题
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.
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.
Update:
I created an example project available on GitHub showing the implementation kindly described by Nicolas Bachschmidt.
回答1:
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.
来源:https://stackoverflow.com/questions/11193804/cocoa-how-to-bind-a-boolean-property-to-nscellstatevalue