NSStatusItem in NSStatusBar, action select method not responding

First, I declare a status element:

var status_item: NSStatusItem?

      

Then I have a function to close the widow and add a state element:

self.view.window?.orderOut(self)
//self.view.window?.close()

self.status_item = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength)
if let status_button = self.status_item?.button {
    status_button.image = NSImage(named: "StatusBarButtonImage")
    status_button.action = #selector(statusBar(sender:))
}

      

Here's my method for selecting an action that should remove the state element and show the window again. This is not called when clicking on the status bar in the status bar:

func statusBar(sender: AnyObject) {
    print("status bar clicked")
    self.status_item = nil
    self.view.window?.orderFront(nil)
}

      

Dose, does anyone know what I am doing wrong?

+3


source to share


1 answer


Set the target button to yourself. I am assuming that you have moved the code from AppDelegate to a separate class. If so, the button is still receiving messages from the AppDelegate.

So...



if let status_button = self.status_item?.button {
    status_button.image = NSImage(named: "StatusBarButtonImage")
    status_button.action = #selector(statusBar(sender:))
    status_button.target = self //critical line
}

      

+3


source







All Articles