Cocoa Bindings:如果多个文本字段都有值,则启用按钮
我有 5 个按钮和 5 个文本字段。我想设置绑定,以便仅当文本字段 2-5 有值时才启用按钮 1...仅当文本字段 1 和 5 具有值时才会启用按钮 2。 3-5 有值等。
我刚刚开始学习可可绑定。我希望我可以使用它们来管理按钮的启用/禁用。当我尝试在其中一个按钮上设置“启用”绑定时,它不允许我引用文本字段。
这可能(并且正确)与绑定有关吗?
I have 5 buttons and 5 text fields. I'd like to set up bindings so that button 1 is enabled only if textfields 2-5 have values..... button 2 is enabled only if textfield 1 & 3-5 have values, etc.
I'm just starting to learn about cocoa bindings. I was hoping I could use them to manage the enabling/disabling of the buttons. When I tried to set up an Enable binding on one of the buttons, it won't let me reference the textfields.
Is this possible (and right) to do with bindings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将无法直接在 bitton 的绑定中引用文本字段。
解决此问题的一种方法是创建一个 NSObject 子类 - 将其称为 ButtonController - 并为 5 个文本字段提供实例变量:
在 xib 文件中,实例化 ButtonController 的对象。现在您可以将事物绑定到按钮控制器实例。
接下来,将所有文本字段绑定到相应的 NSString 属性。
然后你可以将按钮的enabled属性绑定到ButtonController的属性上。当您绑定button1的enabled属性时,将其绑定到text2并选中valuetransformer框并选择“NSISNotNil”。一旦你这样做了,第二个enabled2绑定就变得可用,你可以将它绑定到text3等。
我认为这会起作用,但还没有尝试过。
You won't be able to reference the textfields directly in the bindings for the bitton.
A way to work around this is to create a NSObject subclass - call it ButtonController - and have instance vars for the 5 textfields:
In the xib file, instantiate an object for the ButtonController. Now u can bind things to the button controller instance.
Next, bind all the textfields to the corresponding NSString properties.
Then u can bind the enabled property of the button to ButtonController's properties. When u bind the enabled property of button1, bind it to text2 and check the box for valuetransformer and select "NSIsNotNil". Once u do that, a second enabled2 binding becomes available and u can bind it to text3 etc.
I think this will work but havent tried it yet.
绑定不是有条件的,或者至少在这方面是相当不透明的。您必须在代码中执行此操作。检查您的文本字段文本更改回调并相应地启用按钮。应该只是几行代码。
Bindings are not conditional, or at least rather opaque in this respect. You will have to do this in code. Check your text fields text change callbacks and enable the buttons accordingly. Should be just a few lines of code.