Binding an NSMenuItem's title breaks enabled/disabled validation

99封情书 提交于 2019-12-12 10:37:47

问题


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).

Bug? What to do?


回答1:


For some reason when you set a menu item's title with bindings, the menu item becomes enabled even if the target/action are nil.

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.



来源:https://stackoverflow.com/questions/3337233/binding-an-nsmenuitems-title-breaks-enabled-disabled-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!