如何获取应用程序菜单的NSMenu或NSMenuItem(苹果菜单旁边菜单栏中的菜单)。它似乎是自动创建的,并且独立于我通过NSApplication setMainMenu设置的NSMenu。
顺便说一下:我正在构建没有Xcode的完整应用程序,所以请不要使用InterfaceBuilder提示。
PS:MacOSX 10.5
答案 0 :(得分:18)
如果没有IB,您可以使用NSApplication的mainMenu访问菜单:
NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
NSMenu *appMenu = [[mainMenu itemAtIndex:0] submenu];
for (NSMenuItem *item in [appMenu itemArray]) {
NSLog(@"%@", [item title]);
}
答案 1 :(得分:12)
虽然这是5年的问题......我想分享如何制作它。
根据我在使用Xcode 7.1的OS X 10.11(El Capitan)中的经验,复制该应用程序菜单并不困难。 Apple似乎删除了所有奇怪的限制。
注意:此代码针对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()
无论如何,它不会自动生成,我必须自己设置它们。我不确定是否还有其他办法可以做到这一点。
您可以下载工作示例here。
答案 2 :(得分:1)
制作一个没有Xcode或IB的Cocoa应用程序听起来对我来说是自虐的,但对于他自己的每一个......试试这个:[[[NSApp mainMenu] itemAtIndex: 0] submenu]
。
答案 3 :(得分:0)
我为Swift 5.0支付2美分
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
}