如何在 Swift 中获取主 NSMenu?

问题描述 投票:0回答:1

我想在 Swift 中获取

main NSMenu
以执行以下代码
NSMenu(title: "ApplicationName").autoenablesItems = false
。我不知道插入什么作为
title
,因为它似乎无法获得对它的引用并禁用
autoenablesItems
,因为我无法设置
MenuItem
enabled
-Property。

swift cocoa nsmenu
1个回答
0
投票

只是一个简短的评论,希望它能帮助其他有同样问题的人。这道题的答案是将storyboard中自动创建的主菜单或菜单项添加到appDelegate中,并创建相关代码

class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var saveMenuItem: NSMenuItem! // Can be added by dragging from the storyboard

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
        
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.