Home > Software engineering >  menuItem Action not executed in a swift app
menuItem Action not executed in a swift app

Time:12-09

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: enter image description here

enter image description here

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)
  • Related