I have a swift app for MacOS, and have a menu with sub menuitems. I add the menu from the appdelegate and assign an action via the interface builder, but the target action is never called:
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let menu = menu
{
statusItem?.menu = menu
menu.delegate = self
}
pauseMenuItem.target = self
pauseMenuItem.action = #selector(pausePressed(_:))
We can see from the bullet on the left of IBAction and from the InterfaceBuilder that the link is well done, but whenever I press on the corresponding menuitem, the action is not executed:
What am I missing ?
CodePudding user response:
I ended up creating the menu programmatically and it worked:
let menu = NSMenu()
let pauseButton = NSMenuItem(title: "Pause", action: #selector(pausePressed), keyEquivalent: "")
menu.addItem(pauseButton)