简体   繁体   English

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

[英]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 . 我目前正在使用NSPopoverNSStatusItem

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.actionNSStatusItem.titleNSStatusItem.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.

相关问题 StatusItem的操作在Swift中不起作用 - Action of StatusItem not working in Swift 右键单击NSStatusBarButton时调用Action - Call Action when NSStatusBarButton is right-clicked Cocoa-为什么我要把我的 statusItem 放在 ApplicationDelegate 中? - Cocoa-Why should I put my statusItem in ApplicationDelegate? 单击停靠图标时,可以将emacs配置为运行自定义功能吗? - Can I configure emacs to run a custom function when its dock icon is clicked? NASM - 如何动态加载库并调用其功能 - NASM - How can I dynamically load a library and call its function 如何从 Swift 中的不同类调用函数? - How can I call a function from a different class in Swift? 从 cocoa 应用程序单击菜单时如何处理 window 的关闭操作并具有 Window 的单个实例 - How to handle close action of window and have single instance of Window when clicked on menu from cocoa app 尽管有设置动作,但单击细分时仍显示NSSegmentedControl菜单 - Show NSSegmentedControl menu when segment clicked, despite having set action 如何停止每个函数调用来创建新词典以减少内存使用? - How can I stop creating a new dictionary every function call to reduce memory usage? 如何调用从命令行输入文本的Matlab函数? - How can I call a Matlab function that takes text input from the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM