Search code examples
swiftnsstatusitem

NSMenu and NSStatusItem action wont work together


The button attached to NSStatusItem is not sending calls to its target selector.

I'm not sure what exactly I am doing wrong. I believe its something to do with the button.action line.

let statusItem = NSStatusBar.system.statusItem(withLength:NSStatusItem.squareLength)
let popover = NSPopover()

func applicationDidFinishLaunching(_ aNotification: Notification) {
    if let button = statusItem.button{
        button.image = NSImage(named:NSImage.Name("StatusBar"))
        button.action = #selector(someAction(_:)) //need help here

    }
    popover.contentViewController = QuotesViewController.freshController()
    constructMenu();

}

@objc func someAction(_ sender: Any?){
    print("working");
}

i was following this tutorial - raywenderlich

Edit

This process of setting actions workes for all of the NSMenuItems that I have. I just still the issue with the NSStatusItem.

Edit 2

The code displayed I believe is correct, I now think there's another problem somewhere else.

Edit 3 Answer

"If the status item has a menu set, the action is not sent to the target when the status item is clicked; instead, the click causes the menu to appear."- appledev NSStatusItem.action


Solution

  • "If the status item has a menu set, the action is not sent to the target when the status item is clicked; instead, the click causes the menu to appear."- apple dev NSStatusItem.action