繁体   English   中英

单击statusItem时如何调用函数/动作?

[英]How can I call a function/action when a statusItem is clicked?

我有这段代码可以在“发件人”位置打开弹出元素,即按下的按钮。 单击statusItem时如何调用此函数,以便从状态/菜单栏弹出弹出窗口?

@IBAction func togglePopover(sender: AnyObject) {
    if !(popoverIsOpen) {
        myPopover.showRelativeToRect(sender.bounds, ofView: popoverButton, preferredEdge: NSRectEdge(3))
        popoverIsOpen = true
    }
    else {
        myPopover.close()
        popoverIsOpen = false
    }
}

我目前正在使用NSPopoverNSStatusItem

编辑:针对Xcode 6 beta 4的NSStatusItem.button日志添加了NSStatusItem.button并轻轻弃用了先前的调用形式,例如NSStatusItem.actionNSStatusItem.titleNSStatusItem.target等。

该文档现在显示为

NSStatusItem.button

状态栏中显示的按钮。 这是在创建StatusItem时自动创建的。 可以使用此属性设置按钮的行为自定义,例如图像,目标/操作,工具提示。

我可以使用NSStatusBarItem的新NSStatusBarButton可视表示形式实现以下所示的实现。 在此示例中,我的NSPopover文件具有已连接到视图的NSPopover元素,此处未显示。

@IBOutlet weak var myPopover: NSPopover!
var statusBar: NSStatusItem!
var popoverIsOpen = false

@IBAction func togglePopover(sender: AnyObject) {
    if !(popoverIsOpen) {
        myPopover.showRelativeToRect(sender.bounds, ofView: statusBar.button, preferredEdge: NSRectEdge(3))
        popoverIsOpen = true
    }
    else {
        myPopover.close()
        popoverIsOpen = false
    }
}

func applicationDidFinishLaunching(aNotification: NSNotification?) {
    //initialize menu bar icon
    statusBar = NSStatusBar.systemStatusBar().statusItemWithLength(CGFloat(48))
    statusBar.button.title = "Your App Title"
    statusBar.button.appearsDisabled = false
    statusBar.button.action = Selector("togglePopover:")
    statusBar.button.target = self
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM