简体   繁体   English

当整数等于OSX中的某个特定值时,如何绑定控件中的隐藏属性?

[英]How to bind hidden property in a control when a integer equals some certain value in OSX?

I've got a RadioGroup with 3 cells. 我有一个有3个单元格的RadioGroup。 I want to hide some controls when the selected index in radio group is 1. That is: 当无线电组中的选定索引为1时,我想隐藏一些控件。这是:

[someControl setHidden: radioGroup.selectedIndex == 1];

I've got a lot of controls will show/hidden when radio group selection changed. 当无线电组选择改变时,我会有很多控件显示/隐藏。 Some might show when the selected index equals 0, some might show when equals 2. 有些人可能会在所选索引等于0时显示,有些人可能会在等于2时显示。

I want it to be done by binding, not connect each control reference using outlet. 我希望它通过绑定来完成,而不是使用outlet连接每个控件引用。

How to acheive that? 如何实现?

There are at least two ways of doing this, as binding hidden requires a Boolean balue: 至少有两种方法可以做到这一点,因为绑定hidden需要布尔值:

  • Create a property that is of type BOOL and returns YES or NO based on your value comparison, then in your class use KVO to observe the original value and set the Boolean property inside of the KVO observer (this is required to make sure the object is updated at the right time) 创建一个BOOL类型的属性,并根据您的值比较返回YES或NO,然后在您的类中使用KVO观察原始值并在KVO观察者内部设置Boolean属性(这是确保对象为在合适的时间更新)

  • Use bindings alone, but create a Value Transformer to transform each value you need into a BOOL as necessary to be interpreted correctly. 单独使用绑定,但创建一个Value Transformer,根据需要将所需的每个值转换为BOOL,以便正确解释。 There is an existing value transformer that changes YES to NO and vice-versa, but for other value transforms you will have to create these yourself, and there is no good way to parametrize them inside of the xib file. 现有的值转换器将YES更改为NO,反之亦然,但对于其他值转换,您必须自己创建这些转换器,并且没有好的方法可以在xib文件中对它们进行参数化。

The first solution is probably easier. 第一种解决方案可能更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM