Reputation: 121
I'm writing an app with Swift targeted to macOS Sierra.
I have an NSMenuItem that I'd like to gray out (so the user can't even select it).
I have set its parent menu AutoEnableItems to FALSE in the Storyboard and verified that it's still false via logging in my code.
When I set the isEnabled property of my NSMenuItem to false, what it does is that my action associated with the menu item isn't called (which is good) when I select that menu item, but the menu remains selectable.
What I'd like is for it to be grayed out, and obviously not selectable by the user.
Upvotes: 1
Views: 1630
Reputation: 291
Based on the documentation, what you're doing does sound correct.
However, the way I do it is to implement validateMenuItem
in my AppDelegate class. This method is a little bit annoying because it gets called for every menu item, with the menu item being passed as a parameter. So you have to switch on the parameter, see if it's the menu item you want to disable, and return false.
There's an Objective-C example here: https://developer.apple.com/documentation/objectivec/nsobject/1518160-validatemenuitem
A Swift example might look like:
override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
switch menuItem.tag {
case MenuItemTags.SignOut:
return signOutEnabled()
case MenuItemTags.CheckForUpdates:
return updatesEnabled()
default:
return true
}
}
You may have read this already, but lots of gory details here: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/MenuList/Articles/EnablingMenuItems.html
However, as I said, this article would seem to indicate what you're trying is correct, so who knows? I can say for sure my way works.
Upvotes: 1