Menu di bilah status sistem dinonaktifkan

Dec 23 2020
 func createStatusItem() -> NSStatusItem {
        let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
        item.button?.image = NSImage(systemSymbolName: "archivebox.fill", accessibilityDescription: "")
        item.menu = createMenu()
        return item;
    }

    func createMenu() -> NSMenu {
        let menu = NSMenu(title: "Cap Status Bar Menu")

        menu.addItem(
                withTitle: "Order a burrito",
                action: #selector(StatusBarFactory.x(_:)),
                keyEquivalent: "A")

        menu.addItem(
                withTitle: "Cancel burrito order",
                action: #selector(StatusBarFactory.x(_:)),
                keyEquivalent: "B")

        menu.addItem(NSMenuItem.separator())

        let i = NSMenuItem(title: "test", action: #selector(StatusBarFactory.x(_:)), keyEquivalent: "T");
        menu.addItem(i)
        return menu
    }

Ini membuat menu di bilah status sistem tetapi semua item DINONAKTIFKAN!

Apakah ada yang tahu cara memperbaikinya? Terima kasih banyak.

Jawaban

Asperi Dec 23 2020 at 12:11

Validasi menu tidak menemukan pelaksana tindakan tertentu dalam rantai tanggapan jadi nonaktifkan. Anda harus menentukan target untuk setiap item menu:

    let item = menu.addItem(
            withTitle: "Order a burrito",
            action: #selector(StatusBarFactory.x(_:)),
            keyEquivalent: "A")
    item.target = _instance_of_StatusBarFactory_here // like StatusBarFactory.shared