[英]How can I call a function/action when a statusItem is clicked?
I have this code that opens up a popover element at the "sender" location, ie the button that was pressed. 我有这段代码可以在“发件人”位置打开弹出元素,即按下的按钮。 How can I make this function call when a statusItem is clicked, so that the popover comes down from the status/menu bar? 单击statusItem时如何调用此函数,以便从状态/菜单栏弹出弹出窗口?
@IBAction func togglePopover(sender: AnyObject) {
if !(popoverIsOpen) {
myPopover.showRelativeToRect(sender.bounds, ofView: popoverButton, preferredEdge: NSRectEdge(3))
popoverIsOpen = true
}
else {
myPopover.close()
popoverIsOpen = false
}
}
I am currently using NSPopover
and NSStatusItem
. 我目前正在使用NSPopover
和NSStatusItem
。
edit: The changelog for Xcode 6 beta 4 added NSStatusItem.button
and softly deprecated the previous form of calls like NSStatusItem.action
, NSStatusItem.title
, NSStatusItem.target
, etc. 编辑:针对Xcode 6 beta 4的NSStatusItem.button
日志添加了NSStatusItem.button
并轻轻弃用了先前的调用形式,例如NSStatusItem.action
, NSStatusItem.title
, NSStatusItem.target
等。
The documentation now reads 该文档现在显示为
NSStatusItem.button
The button that is displayed in the status bar. 状态栏中显示的按钮。 This is created automatically on the creation of the StatusItem. 这是在创建StatusItem时自动创建的。 Behavior customization for the button, such as image, target/action, tooltip, can be set with this property. 可以使用此属性设置按钮的行为自定义,例如图像,目标/操作,工具提示。
I was able to reach an implementation shown below, using the new NSStatusBarButton visual representation of an NSStatusBarItem. 我可以使用NSStatusBarItem的新NSStatusBarButton可视表示形式实现以下所示的实现。 In this example, my .xib file has the NSPopover
element already connected to a view, which isn't shown here. 在此示例中,我的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.