Cannot seem to setEnabled:NO on NSMenuItem

笑着哭i 提交于 2019-12-04 00:12:01

Had the same issue, so I thought I'd post my solution. NSMenu auto enables NSMenuButtons, so we have to override that.

In IB:

Or programmatically:

// Disable auto enable
[myMenu setAutoenablesItems:NO];

// Test it
[myMenuButton setEnabled:NO];
[myMenuButton setEnabled:YES];

I solved it with the help of a colleague, so I post it here for others that experience the same issue.

You should set your NSMenu-sublass to auto-enable items (default behaviour) and then implement this method in the NSMenu-sublass.

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

You should uncheck Auto Enables Items on the closest parent NSMenu

You can solve this problem without subclassing.

If only you need is standard menu + some custom NSMenuItems that can be disabled on your control, than you can just:

1) Add one more menu node - just drag&drop Submenu Menu Item from Object library to your menu.

2) Add all NSMenuItems you want to manage to this new Menu node.

3) Open Attributes inspector for your New Menu node, and switch off Auto Enables Items option:

http://i.imgur.com/YLZKFam.jpg (please, see image here, I can't post it directly now).

4) configurate other options of your menu & items.

Now you can write a code like:

@property (weak) IBOutlet NSMenuItem *hidePlateMenuItem;
...
[self.hidePlateMenuItem setEnabled:NO];

and it wil works well .

Adding to the response of itsdavyh, if the menu item is located inside one or more submenus you only have to uncheck the 'Auto enables items'-property on the submenu of the menu item and not on any other parentmenus.

Try calling [myMenuItem setEnabled:NO] from a different place and making sure that it happens after the menu-containing nib is loaded. Maybe do it right in the subclassed NSMenu's awakeFromNib.

I tried all these solution but finally i found the real problem that also make more sense to me. It also is the simplest way to handle disabled nsmenuitem that no needs to subclass or do code. The nsmenuitem before to be child of menu itself is child of the main item for example "Save as..." is child of "File". Just select the parent item (in this example is File) and set off "auto enable menu items" in the menu ispector panel, and here you go!

Swift 3 answer:

I have a submenu item under the standard "View" menu called "Enable System Setup On Launch". I use the function below to enable or disable the menu item. Note: the view menu does need the "Auto Enable Items" in IB to be turned off.

func enableSystemSetupMenuItem(enabled:Bool) {
    //set view menu item to enabled: value
    //requires "Auto Enable Items" of "View" menu item to be turned off in IB
    //because "View" menu is now turned off for "Auto Enable" we have to handle all 
    //of the "View" menu items ourselves
    //just to avoid operating on menu separators I set all other menu items to TAG = -1
    let main = NSApplication.shared().menu?.item(withTitle: "View")
    let subMenuItems = main?.submenu?.items
    for item in subMenuItems! {
        if item.title == "Enable System Setup On Launch" {
            item.isEnabled = enabled
        } else if item.tag == -1 {
            item.isEnabled = true
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!