本文介绍了从Swift中的NSStatusItem(菜单栏)应用程序访问菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在创建一个应用程序,在该应用程序中,菜单栏似乎是使用户的桌面清洁而没有窗口的最便捷方法.我在网上和堆栈上都看过许多教程,但它们似乎仅适用于Objective-C.我只使用Swift.如果您不知道菜单栏是什么,它们就是这些图标:

我希望我的应用具有其中之一,而不是一个恒定的全窗口.如果可以的话,如何在菜单栏上有一个弹出窗口的按钮.最后,如何显示我的图标,但仍然具有取景器的优势. (如文件,编辑..).例如,

我已经试过放

Application is Agent (UIElement) to False

在我的Info.plist中,但这也剥夺了我的发现者优势.

解决方案

大概是您想说的是要使编辑"菜单中的文本编辑项(如撤消",剪切",复制",粘贴",全选")起作用在您应用的窗口中.

那些菜单项是另一个应用程序的一部分,并且仅在该应用程序中发送消息.它们对您的应用程序不可用,无论它是否是代理"(本身没有可见的菜单栏).如果您的代理应用程序窗口之一是键窗口,并且用户单击了另一个应用程序的菜单标题(如文件"或编辑"),则该应用程序将被激活,并且您的应用程序窗口将退出"键窗口状态. /p>

可以使通常的快捷键(如Cut的)适用于您的应用,这很容易.当应用程序的一个窗口是用户的键窗口时,您的应用程序会接收键盘事件,即使未显示主菜单,您的NSApplication对象(自动为您创建)也会检查其mainMenu的键盘快捷方式在屏幕上.

OS X"Cocoa Application"项目模板在MainMenu.xib(或Main.storyboard)中为您设置了一个主菜单栏,所有菜单项均已连接至适当的操作.因此,如果保留主菜单栏和编辑"菜单以及编辑"菜单中的菜单项,并保留在这些菜单项上设置的快捷方式,则即使您在LSUIElement设置为YES,键盘快捷方式也将起作用>,则您的应用程序窗口之一就是关键窗口.换句话说,快捷键在默认情况下将起作用,并且您必须进行更改以使它们停止起作用.

应用程序窗口中的文本字段仍将具有默认的右键单击菜单,其中包含诸如剪切,复制和粘贴之类的常用项目,因此您无需执行任何其他操作即可使该工作正常进行.

这是我的测试应用程序MainMenu.xib的内容:

我已经不理会主菜单栏了.我创建了一个单独的菜单,其中包含显示窗口"和退出"两个项目.我已将退出"的快捷方式设置为,但是此快捷方式无效.主菜单栏上的StatusItem>退出菜单项(在我的屏幕快照中不可见)具有相同的快捷方式设置,这就是重要的设置.我在另一个退出"项上设置了快捷方式,因为它对用户可见,而主菜单栏对用户不可见.

我已将此退出项目关联到第一响应者"的terminate:操作. (默认情况下,StatusItem> Quit菜单项的连接方式相同.)

这是我的AppDelegate:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet var window: NSWindow!
    @IBOutlet var statusItemMenu: NSMenu!

    var statusItem: NSStatusItem?

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)
        let statusItem = self.statusItem!
        let button = statusItem.button!
        button.title = "Hello"
        statusItem.menu = statusItemMenu
    }

    @IBAction func showWindow(sender: AnyObject) {
        NSApp.activateIgnoringOtherApps(true)
        window.makeKeyAndOrderFront(sender)
    }

}

我已将显示窗口"菜单项连接到showWindow(_:)动作,并且已将statusItemMenu插座连接到XIB中的独立菜单.

我还将Info.plist中的应用程序是代理程序(UIElement)"设置为是".

当我运行此应用程序时,它将在菜单栏中创建状态项.我可以从项目中选择显示窗口",我的窗口位于最前面并成为关键.我可以右键单击文本字段以获取其上下文菜单.我可以使用标准的快捷方式剪切/复制/粘贴/等等,以关闭窗口,甚至退出应用程序.

I'm creating an application where a menubar seems to be the most convenient way to have the user's desktop clean without a window. I've seen many tutorials online and on stack overflow but they seem to be only for Objective-C. I only use Swift. If you don't know what a menubar is, they're these icons:

I would like my app to have one of these instead of a constant full window. And if I can, how can I have a button on my menubar that brings up the window. Lastly, how can I have my icon not show, but I still have the finder advantages. (Like File, Edit..). For example,

I have already tried to put

Application is Agent (UIElement) to False

in my Info.plist but that also takes away my finder advantages.

解决方案

Presumably what you're saying is that you want the text editing items (like Undo, Cut, Copy, Paste, Select All) from the Edit menu to work in your app's window.

Those menu items are part of another application, and only send messages in that application. They aren't available to your application, regardless of whether it's an "agent" (with no visible menu bar of its own). If one of your agent app's windows is the key window and the user clicks on a menu title (like File or Edit) that belongs to another app, then that app will activate and your app's window will "resign" the key window status.

You can make the usual shortcut keys (like for Cut) work for your app, and it's easy. When one of your app's windows is the user's key window, your app receives keyboard events, and your NSApplication object (created for you automatically) will check its mainMenu for keyboard shortcuts even though the main menu is not displayed on the screen.

The OS X "Cocoa Application" project template sets up a main menu bar for you in MainMenu.xib (or in Main.storyboard), with all of the menu items wired up to the appropriate actions. So if you keep that main menu bar and the Edit menu and the menu items in the Edit menu and leave the shortcuts set on those items, then the keyboard shortcuts will work even if you set LSUIElement to YES in your Info.plist, when one of your app's windows is the key window. In other words, the shortcut keys will work by default, and you have to change things to make them stop working.

Text fields in your app's windows will also still get the default right-click menu with the usual items like Cut, Copy, and Paste, so you don't need to do anything else to make that work either.

Here's the contents of my test app's MainMenu.xib:

I've left the main menu bar alone. I've created a separate menu with two items, "Show Window" and "Quit". I've set the shortcut for "Quit" to , but this shortcut has no effect. The StatusItem > Quit menu item (not visible in my screen shot) off the main menu bar has the same shortcut set, and that's the setting that matters. I've set the shortcut on this other Quit item because it's visible to the user, and the main menu bar won't be visible to the user.

I've wired this Quit item to the terminate: action of First Responder. (The StatusItem > Quit menu item is connected the same way by default.)

Here's my AppDelegate:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet var window: NSWindow!
    @IBOutlet var statusItemMenu: NSMenu!

    var statusItem: NSStatusItem?

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)
        let statusItem = self.statusItem!
        let button = statusItem.button!
        button.title = "Hello"
        statusItem.menu = statusItemMenu
    }

    @IBAction func showWindow(sender: AnyObject) {
        NSApp.activateIgnoringOtherApps(true)
        window.makeKeyAndOrderFront(sender)
    }

}

I've wired the "Show Window" menu item to the showWindow(_:) action, and I've connected the statusItemMenu outlet to that standalone menu in the XIB.

I also set "Application is Agent (UIElement)" to "YES" in Info.plist.

When I run this app, it creates the status item in the menu bar. I can choose "Show Window" from the item and my window comes to the front and becomes key. I can right-click the text field to get its context menu. I can use the standard shortcuts to cut/copy/paste/etc., to close the window, and even to quit the app.

这篇关于从Swift中的NSStatusItem(菜单栏)应用程序访问菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:40