问题
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