问题描述
我有一个菜单,其中某些菜单项使用绑定来获取其标题。这些项目始终处于启用状态,并且既不会自动启用/禁用,也不会像它们也不应该导致调用validateUserInterfaceItem:那样自动启用/禁用。如果您删除标题上的绑定,那么它将重新开始工作。菜单项的目标设置为nil(第一响应者)。如果单击一个,它就会执行选择器(操作)。
I have a menu where some of the menu items use bindings to get their title. These items are always enabled, and don't neither automatically enable/disable like they should NOR do they cause a call to validateUserInterfaceItem:. If you remove the binding on title, then that starts working again. The menu items have the target set to nil (First Responder). If you click on one, it does execute the selector (action).
错误吗?
推荐答案
由于某些原因,当您设置带有绑定的菜单项标题时,该菜单项将被启用$ b $即使目标/操作为 nil
。
For some reason when you set a menu item's title with bindings, the menu item becomes enabledeven if the target/action are nil
.
如果要永久禁用菜单项,则可以解决通过将菜单项的已启用
状态绑定到常量 NO
:
If you want to permanently disable the menu item you can workaround this by binding the menu item's enabled
status to a constant NO
:
NSNumber *alwaysNo = [NSNumber numberWithBool:NO];
[menuItem bind:@"enabled" toObject:alwaysNo withKeyPath:@"boolValue" options:nil];
请注意,这不是最优雅的解决方法,但就我而言,它仍然比不干净使用标题的绑定。
Note that this isn't the most elegant workaround, but in my case it was still cleaner than not using bindings for the title.
这篇关于绑定NSMenuItem的标题会中断启用/禁用的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!