【发布时间】:2021-08-19 15:35:11
【问题描述】:
我正在尝试使用 NSMenu 类通过 Appkit 将菜单添加到应用程序菜单。但到目前为止我还不确定如何称呼它:
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
var menu: NSMenu!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 512),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.isReleasedWhenClosed = false
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
class myMenu: NSMenu {
init(aTitle: "HelloWorld")
}
}
但这只会给我“':'之后的预期参数类型”和“'必需'初始化程序'init(coder:)'必须由'NSMenu'的子类提供”。
【问题讨论】:
-
您需要将您的菜单添加到
NSApplication.shared.mainMenu。有关示例,请参阅此问题的答案:stackoverflow.com/questions/28781974/…
标签: swift macos swiftui appkit