[英]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
}
}
我目前正在使用NSPopover
和NSStatusItem
。
編輯:針對Xcode 6 beta 4的NSStatusItem.button
日志添加了NSStatusItem.button
並輕輕棄用了先前的調用形式,例如NSStatusItem.action
, NSStatusItem.title
, NSStatusItem.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.