NSStatusBar中的NSStatusItem,动作选择器方法未响应 [英] NSStatusItem in NSStatusBar, action selector method not responding

查看:100
本文介绍了NSStatusBar中的NSStatusItem,动作选择器方法未响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我声明状态项:

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?

推荐答案

将按钮的目标设置为self.我假设您已将代码从AppDelegate移至单独的类.如果是这样,则该按钮仍从AppDelegate接收消息.

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.

所以...

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
}

这篇关于NSStatusBar中的NSStatusItem,动作选择器方法未响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆