如何获取应用程序菜单的 NSMenu 或 NSMenuItem(在苹果菜单旁边的菜单栏中)。它似乎是自动创建的,并且独立于我通过 NSApplication setMainMenu 设置的 NSMenu。
顺便说一句:我正在构建没有 Xcode 的完整应用程序,所以请不要使用 InterfaceBuilder 提示。
PS:MacOSX 10.5
如果没有 IB,您可以使用 NSApplication 的 mainMenu 访问菜单:
NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
NSMenu *appMenu = [[mainMenu itemAtIndex:0] submenu];
for (NSMenuItem *item in [appMenu itemArray]) {
NSLog(@"%@", [item title]);
}
虽然这是 5 年前的问题......我喜欢分享如何制作它。
根据我在使用 Xcode 7.1 的 OS X 10.11 (El Capitan) 的经验,复制该应用程序菜单并不难。苹果似乎消除了所有奇怪的限制。
注意:此代码针对 Swift 3 进行了更新,并且仅在 macOS Sierra (10.12.1) 中进行了测试。
//
// AppDelegate.swift
// Editor6MainMenuUI2Testdrive
//
// Created by Hoon H. on 2016/11/05.
// Copyright © 2016 Eonil. All rights reserved.
//
import Cocoa
/// You SHOULD NOT use `@NSApplicationMain`
/// to make your custom menu to work.
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {}
func applicationWillTerminate(_ aNotification: Notification) {}
}
func makeMainMenu() -> NSMenu {
let mainMenu = NSMenu() // `title` really doesn't matter.
let mainAppMenuItem = NSMenuItem(title: "Application", action: nil, keyEquivalent: "") // `title` really doesn't matter.
let mainFileMenuItem = NSMenuItem(title: "File", action: nil, keyEquivalent: "")
mainMenu.addItem(mainAppMenuItem)
mainMenu.addItem(mainFileMenuItem)
let appMenu = NSMenu() // `title` really doesn't matter.
mainAppMenuItem.submenu = appMenu
let appServicesMenu = NSMenu()
NSApp.servicesMenu = appServicesMenu
appMenu.addItem(withTitle: "About Me", action: nil, keyEquivalent: "")
appMenu.addItem(NSMenuItem.separator())
appMenu.addItem(withTitle: "Preferences...", action: nil, keyEquivalent: ",")
appMenu.addItem(NSMenuItem.separator())
appMenu.addItem(withTitle: "Hide Me", action: #selector(NSApplication.hide(_:)), keyEquivalent: "h")
appMenu.addItem({ () -> NSMenuItem in
let m = NSMenuItem(title: "Hide Others", action: #selector(NSApplication.hideOtherApplications(_:)), keyEquivalent: "h")
m.keyEquivalentModifierMask = [.command, .option]
return m
}())
appMenu.addItem(withTitle: "Show All", action: #selector(NSApplication.unhideAllApplications(_:)), keyEquivalent: "")
appMenu.addItem(NSMenuItem.separator())
appMenu.addItem(withTitle: "Services", action: nil, keyEquivalent: "").submenu = appServicesMenu
appMenu.addItem(NSMenuItem.separator())
appMenu.addItem(withTitle: "Quit Me", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
let fileMenu = NSMenu(title: "File")
mainFileMenuItem.submenu = fileMenu
fileMenu.addItem(withTitle: "New...", action: #selector(NSDocumentController.newDocument(_:)), keyEquivalent: "n")
return mainMenu
}
let del = AppDelegate()
/// Setting main menu MUST be done before you setting app delegate.
/// I don't know why.
NSApplication.shared().mainMenu = makeMainMenu()
NSApplication.shared().delegate = del
NSApplication.shared().run()
无论如何,它不是自动生成的,我必须自己设置它们。我不确定是否有其他方法可以做到这一点。
您可以在此处下载工作示例。
Swift 5.0 我的两分钱
private final func manageMenus(){
let mainMenu = NSApplication.shared.mainMenu
if let editMenu = mainMenu?.item(at: 1)?.submenu{
for item in editMenu.items{
print(item.title)
}
}
}
所以你也可以启用它:
……
for item in editMenu.items{
item.isEnabled = true
}
制作一个没有 Xcode 或 IB 的 Cocoa 应用程序对我来说听起来很自虐,但对每个人来说都是他自己的......试试这个:[[[NSApp mainMenu] itemAtIndex: 0] submenu]
。