Cocoa: How to bind a boolean property to NSCellStateValue?

此生再无相见时 提交于 2019-12-10 14:38:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!