menu item is enabled, but still grayed out

断了今生、忘了曾经 提交于 2020-04-08 02:49:00

问题


I have a menu with several items created in interface builder. It looks fine there and 'enabled' is checked.
But when I run application, all menu items are grayed out.

I've checked isEnabled, it returns true.

Also, menu items created programmatically (with initWithTitle and without interface builder) work just fine.

Am I missing something here? I'm really quite new to OSX development (in fact, this is my first day).
Thank you


回答1:


Remember to set your menu item's target and ensure that said target implements the menu item's action method.

menuItem.target = self;
  • If the menu item’s target is set, then NSMenu first checks to see if that object implements the item’s action method. If it does not, then the item is disabled. If the target does implement the item’s action method, NSMenu first checks to see if that object implements validateMenuItem: or validateUserInterfaceItem: method. If it does not, then the menu item is enabled. If it does, then the enabled status of the menu item is determined by the return value of the method.

  • If the menu item’s target is not set and the NSMenu object is not a contextual menu, then NSMenu uses the responder chain to determine the target. If there is no object in the responder chain that implements the item’s action, the item is disabled.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MenuList/Articles/EnablingMenuItems.html




回答2:


In case somebody might google this out and benefit, 'Action' method was declared without :(id)sender parameter:

-(IBAction) quit;

Strangely, setAction method in NSMenuItem ate it and didn't complain. Oh well.




回答3:


Ah, the plague of using NSMenu...

Check out <NSMenuValidation>.

Usually the implementation will be as simple as:

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
  return [menuItem isEnabled];
}


来源:https://stackoverflow.com/questions/4870141/menu-item-is-enabled-but-still-grayed-out

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