Reputation: 5283
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
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: ]
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: ]
Upvotes: 1