Search code examples
swiftmacosnsmenuitem

How to enable NSMenuItems in Mac OS?


I've been creating a Mac OS application in swift. When I create my own custom menu items, they stay disabled throughout the lifespan of the app. How do I enable these menu items to make them clickable? Link to example image - https://i.sstatic.net/00ivR.jpg

Edit: I found that setting up IBActions in the window controller and then accessing them through the responder chain worked.


Solution

  • I make this mistake all the time. ;)

    To enable a menu item in Cocoa, you either have to set the container menu's autoenablesItems=false and then enable/disable the individual items manually.

    Or you have to set the action and target properties of the menu items and make sure these are being recognized in the appropriate -validateUserInterfaceItem: handler in the responder chain.

    If you don't do either of these, auto-enable is on by default and none of your items have valid action targets, so the menu disables them all.