Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot seem to setEnabled:NO on NSMenuItem

I have subclassed NSMenu and connected a bunch of NSMenuItem's via Interface Builder. I have tested via the debugger to see that they really get initialized.

The menu is set to not auto enable items. Still when I set any of my NSMenuItem's to [myMenuItem setEnabled:NO] they continue to be enabled. Even if I call [self update] from within the NSMenu subclass.

What am I missing?

like image 547
Christoffer Avatar asked Feb 20 '12 21:02

Christoffer


3 Answers

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

In IB:

You should uncheck "Auto Enables Items"

Or programmatically:

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

// Test it
[myMenuButton setEnabled:NO];
[myMenuButton setEnabled:YES];
like image 117
vqdave Avatar answered Oct 22 '22 05:10

vqdave


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];
}
like image 45
Christoffer Avatar answered Oct 22 '22 04:10

Christoffer


enter image description here

You should uncheck Auto Enables Items on the closest parent NSMenu

like image 11
onmyway133 Avatar answered Oct 22 '22 06:10

onmyway133