【问题标题】:How can I call a function/action when a statusItem is clicked?单击状态项时如何调用函数/操作?
【发布时间】:2014-07-25 21:41:23
【问题描述】:

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

@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.actionNSStatusItem.titleNSStatusItem.target 等。

文档现在改为

NSStatusItem.button

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

【问题讨论】:

    标签: macos swift


    【解决方案1】:

    使用 NSStatusBarItem 的新 NSStatusBarButton 可视化表示,我能够实现如下所示。在此示例中,我的 .xib 文件具有已连接到视图的 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
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多