Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSStatusItem in NSStatusBar, action selector method not responding

First I declare the status item:

var status_item: NSStatusItem?

Then I have a function to close the widow and add the status item:

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 action selector method, that should remove the status item and show the window again. Tho it's not being called when pressing the status bar item in the status bar:

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

Dose anyone know what I'm doing wrong?

like image 708
Heestand XYZ Avatar asked May 16 '17 12:05

Heestand XYZ


1 Answers

Set the button's target to self. I'm assuming you've moved code from the AppDelegate to a separate class. If so, the button is receiving messages from AppDelegate still.

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
}
like image 134
laaksom Avatar answered Nov 09 '22 05:11

laaksom