簡體   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