macOS菜单栏App中禁用的菜单项 [英] menu items disabled in macOS menubar App

查看:75
本文介绍了macOS菜单栏App中禁用的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在macOS上构建菜单栏应用程序.

I'm trying to build a menubar app on macOS.

我似乎无法弄清楚为什么某些菜单项被禁用了……屏幕截图:

I can't seem to figure out why some menu items are disabled... Screenshot:

如您所见, Quit 菜单项已启用,并在单击时退出应用程序.但是,首选项项被禁用.

As you can see, the Quit menu item is enabled, and quits the app when clicked. The Preferences item is disabled however.

AppDelegate.swift:

AppDelegate.swift:

let menuBarItem = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength)

func applicationDidFinishLaunching(_ aNotification: Notification) {
    menuBarItem.button?.image = NSImage(named: "MenuBarIcon")
    menuBarItem.menu = MenuBarMenu()
}

MenuBarMenu.swift:

MenuBarMenu.swift:

class MenuBarMenu: NSMenu {
    init() {
        super.init(title: "Menu")
        self.addItem(withTitle: "Preferences...", action: #selector(MenuBarActions.openPreferencesWindow(_:)), keyEquivalent: "")
        self.addItem(NSMenuItem.separator())
        self.addItem(withTitle: "Quit", action: #selector(MenuBarActions.terminate(_:)), keyEquivalent: "")
    }

    required init(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been impemented")
    }
}

class MenuBarActions {
    @objc static func terminate(_ sender: NSMenuItem) {
        NSApp.terminate(sender)
    }

    @objc static func openPreferencesWindow(_ sender: NSMenuItem) {
        print("preferences")
    }
}

我正在使用完全相同的方式来创建MenuBarItems和用于选择器的相同结构,因此这种不一致让我有些困惑.发生这种情况的原因是什么?如何解决此问题?

I am using the exact same way to create both MenuBarItems and the same structure for the selectors, so I am a little confused by this inconsistency. What is the reason this happens and how can I solve this problem?

推荐答案

退出"菜单项正在意外"运行.它没有使用您实现的 terminate(_:)方法.在其中放置 print()语句,您会发现它没有被调用.

Your Quit menu item is working "accidentally". It is not using the terminate(_:) method you implemented. Put a print() statement there and you'll see it's not being invoked.

菜单项要么分配了特定的目标对象,要么使用响应者链搜索适当的目标.您没有为菜单项分配目标,因此它们正在使用响应者链.您的 MenuBarActions class 不属于响应者链.(通常不能是类.某些对象可以是.)因此,菜单项永远不会以您的类为目标.

A menu item either has a specific target object assigned or it uses the responder chain to search for an appropriate target. You are not assigning a target for your menu items, so they are using the responder chain. Your MenuBarActions class is not part of the responder chain. (Classes generally can't be. Certain objects can be.) So, the menu items will never target your class.

退出菜单之所以起作用,是因为该应用程序对象位于响应者链上,并且具有 terminate(_:)方法.实际上,这就是您的 terminate(_:)方法将被调用的地方.但是菜单项实际上是在直接调用它.

The Quit menu works because the application object is on the responder chain and it has a terminate(_:) method. In fact, that's what your terminate(_:) method would invoke if it were ever being called. But the menu item is actually invoking it directly.

您应该创建一个实际的控制器对象(不仅仅是类)来实现操作方法,并为其设置菜单项的 target 属性.

You should create an actual controller object (not just class) that implements the action methods and set the menu items' target property to it.

这篇关于macOS菜单栏App中禁用的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆