可可绑定没有按预期工作?尝试将启用的 NSTextField 绑定到复选框状态
我只是尝试使用复选框来启用/禁用 NSTextField。我在下面详细列出了我尝试的最后一种方法,但我尝试了多种方法:
使用 NSObjectController ,将内容对象绑定到控制器,并将复选框和标签绑定到 NSObjectController 选择,并使用控制器中属性的关键路径。使用和不使用 NSINil Transformer。
没有带有复选框和标签的 NSObjectController 绑定到控制器,且控制器中具有指向 BOOL 属性的关键路径。使用和不使用 NSINil Transformer。
添加了键的 NSObjectController 和“准备内容”设置,其中复选框和标签绑定到 NSObjectController 选择并添加了键路径的键。使用和不使用 NSINil Transformer。
我已经阅读了许多示例并阅读了文档,但没有运气让这个工作正常进行。
- 清洁项目可可项目。
- NSObjectController “对象控制器”添加到 main.xib。
- 将 IB 中的 NSObjectController 设置为“准备内容”,
- 将值“enableText”添加到键中。
- 将复选框和标签拖到视图上。
- 添加绑定到绑定到“对象控制器”的“值”的复选框,其中控制器键“选择”和模态键路径“enableText”。此时自动完成报告“未找到完成”!它应该找不到我添加到对象控制器的那个吗???
- 添加绑定到绑定到“对象控制器”的“已启用”标签,其中控制器键“选择”和模态键路径“enableText”。同上。
运行一下就崩溃了如果我将标签绑定设置为具有 NSINil 转换器或它提供的任何选项,它不会崩溃,但复选框不会更改标签的启用状态。崩溃结果如下。
值得注意的是,如果我还在标签值和与启用的绑定相同的键之间添加绑定,那么当我切换复选框时,标签内容会在 0 和 1 之间切换。所以,这似乎应该很容易适用于启用的绑定......但事实并非如此。
2012-02-20 23:43:58.273 TestBindings2[12346:903] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x100123540
2012-02-20 23:43:58.275 TestBindings2[12346:903] An uncaught exception was raised
2012-02-20 23:43:58.276 TestBindings2[12346:903] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x100123540
2012-02-20 23:43:58.278 TestBindings2[12346:903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x100123540'
使用 XCode 4.2 和 OSX 10.6.8。
I am simply attempting to use a checkbox to enable/disable an NSTextField. I have listed below the last method I attempted in detail, but I have attempted it multiple ways:
Use an NSObjectController with Content Object bound to controller and checkbox and label bound to NSObjectController selection with key path to property in controller. With and without NSIsNil Transformer.
No NSObjectController with checkbox and label bound to controller with key path to BOOL property in controller. With and without NSIsNil Transformer.
NSObjectController with Keys added and "Prepare Content" set with checkbox and label bound to NSObjectController selection and added key for key path. With and without NSIsNil Transformer.
I have gone over numerous examples and reading the documentation and have had no luck getting this working.
- Clean project cocoa project.
- NSObjectController "Object Controller" added to main.xib.
- Set NSObjectController in IB to "Prepares Content"
- Add to Keys the value "enableText".
- Drag a checkbox and label onto view.
- Add binding to checkbox for "Value" bound to "Object Controller", with Controller Key "selection" and Modal Key Path "enableText". At this point the auto completion reports "No completions found"! Should it not find the one I added to Object Controller???
- Add binding to label for "Enabled" bound to "Object Controller", with Controller Key "selection" and Modal Key Path "enableText". Ditto.
Run and it crashes. If I set the label binding to have a Transformer of NSIsNil or any of the options it provides, it does not crash, but then the checkbox does not change the enabled state of the label. The crash result is below.
Of note, is that if I also add a binding between the label value and the same key as the enabled binding, then the label content does toggle between 0 and 1 when I toggle the checkbox. So, this seems like it should easily work for the enabled binding... but it isn't.
2012-02-20 23:43:58.273 TestBindings2[12346:903] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x100123540
2012-02-20 23:43:58.275 TestBindings2[12346:903] An uncaught exception was raised
2012-02-20 23:43:58.276 TestBindings2[12346:903] -[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x100123540
2012-02-20 23:43:58.278 TestBindings2[12346:903] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSControllerObjectProxy copyWithZone:]: unrecognized selector sent to instance 0x100123540'
Using XCode 4.2 and OSX 10.6.8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是尝试以与“启用”完全相同的方式绑定标签的“隐藏”,并且它确实隐藏了它。这让我意识到我在绑定方面做得正确。我尝试手动禁用标签,但发现这没有任何作用。然后我在某处看到它说标签是已启用的 NSTextFields,这表明它们可能无法禁用。果然,将标签更改为文本字段并且功能按预期工作。
Just attempted to bind the Label's "Hidden" in the exact same way as "Enabled", and it did hide it. This made me realize I was doing things correctly with binding. I attempted to manually disable the Label, and saw this did nothing. Then I saw somewhere that it says Labels are NSTextFields that are enabled, which suggests the possibility that they can not be disabled. Sure enough, changing the Label to a Text Field and the functionality works as intended.