mdmb
mdmb

Reputation: 5283

MenuItem disabled after turning off the view

I set up a project with two view controllers connected to two xibs (MainMenu.xib and MasterVC.xib)

In my MasterVC I programatically add NSMenuItems with actions (located in MasterVC) to a menu, that is located in MainMenu.xib (and is connected to AppDelegate).

let menuItem = NSMenuItem()
menuItem.title = object.name!
menuItem.keyEquivalent = object.shortcut!
menuItem.representedObject = object
menuItem.action = "testSelector:"
appDel.menu.insertItem(menuItem, atIndex: 0)

func testSelector(sender: NSMenuItem) {
    let object = (sender.representedObject as! MyNewObject)
    print("Name of set:", object.name)
}

My added menu items work (are enabled) as long as I have window from MasterVC opened. As soon as I close it I can't click those menu items (they are disabled).

Is there a way to keep them enabled all the time?

Upvotes: 1

Views: 159

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

You need to do two steps in your storyboard or XIB file (whichever contains the main menu):

1)

Use the Attribute Inspector after selecting the menu and then turn off "Auto Enable Items" checkbox: Turn off Auto Enable Items for each menu]

2)

And for each menu item you want to have enabled, select that menu item and use the Attributes Inspector to make sure this checkbox is set to on: Manually Enable Each Menu item]

Upvotes: 1

Related Questions