Cocoa 绑定——尝试将 NSMenuItem 的状态绑定到我的自定义 BOOL
在我的 mac 应用程序中,[Model m] 是一个自定义对象,具有合成的 BOOL 属性和 ivar rollAnimations。 AnimationsItem 是一个 NSMenuItem 对象。我想将animationsItem的状态绑定到[Model m]的rollAnimations属性和ivar。双向绑定是理想的(这样改变一个属性就会改变另一个属性),但如果这很混乱(保留循环等),我会选择单向绑定,这样改变菜单项就会改变 rollAnimations财产。
这是一个代码片段。它不起作用。我缺少什么?
NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
NSNumber *yesNumber = [NSNumber numberWithBool:YES];
[bindingOptions setObject:yesNumber forKey:NSValidatesImmediatelyBindingOption];
[animationsItem bind:@"state" toObject:[Model m] withKeyPath:@"rollAnimations" options:bindingOptions];
In my mac app, [Model m] is a custom object with a synthesized BOOL property and ivar rollAnimations. And animationsItem is an NSMenuItem object. I want to bind the state of my animationsItem to the rollAnimations property and ivar of [Model m]. A two-way binding would be ideal (so that changing either property changes the other), but if that's messy (retain cycles and such), I'll settle for a one-way binding, such that changing the menu item changes the rollAnimations property.
Here is a code snippet. It's not working. What am I missing?
NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
NSNumber *yesNumber = [NSNumber numberWithBool:YES];
[bindingOptions setObject:yesNumber forKey:NSValidatesImmediatelyBindingOption];
[animationsItem bind:@"state" toObject:[Model m] withKeyPath:@"rollAnimations" options:bindingOptions];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cocoa 绑定参考列出了 菜单项支持的所有绑定。您想要的是
@"value"
,而不是@"state"
。 (这也适用于按钮 , 顺便一提。)The Cocoa Bindings Reference lists all the bindings a menu item supports. The one you want is
@"value"
, not@"state"
. (This goes for buttons, too, by the way.)