可可:如何将布尔属性绑定到NSCellStateValue? [英] Cocoa: How to bind a boolean property to NSCellStateValue?

查看:119
本文介绍了可可:如何将布尔属性绑定到NSCellStateValue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将NSTextField的布尔enabled属性绑定到NSButton的状态.我已经尝试添加一个自定义NSValueTransformer,它将NSButton的状态转换为NSNumber.但是,在这种情况下,由于某些原因,文本字段始终处于禁用状态.我的第二种方法:失败也会失败,因为NSValueTransformer不提供诸如BOOL之类的返回原语类型.

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.

示例:
屏幕快照显示了一个示例,其中由于复选框的状态为NSOnState,因此禁用了文本字段.我还想将标签绑定到此状态.

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.

此外,如果我可以在Interface Builder中设置禁用的文本" ,那将会很方便.在上面的示例中,我在关联的类中设置了文本.

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.

我将self.anonymousLoginCheckbox.state设置为帐户文本字段的 enabled 属性的 Model Key Path .与密码文本字段类似.但是,它起作用.

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.

更新:

我创建了一个示例项目,该示例项目在GitHub上可用,显示了由Nicolas Bachschmidt亲切描述的实现. >

推荐答案

NSButton与密钥state不兼容.可可绑定要求观察到的对象在观察到的属性更改时发出通知.由于NSButtonstate只是其单元格state的包装,因此当用户单击按钮时,不会调用-[NSButton setState:]方法(以及自动KVO通知)(但-[NSCell setState:]是).如果将模型密钥路径设置为self.anonymousLoginCheckbox.cell.state,它将起作用.

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.

这篇关于可可:如何将布尔属性绑定到NSCellStateValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆