gpt4 book ai didi

macos - 为什么 NSApplication sharedApplication mainMenu 返回 nil ?

转载 作者:行者123 更新时间:2023-12-03 16:40:22 26 4
gpt4 key购买 nike

Apple 的文档说:

All of an application’s menus in the menu bar are owned by one NSMenu instance that’s created by the application when it starts up. You can retrieve this main menu with the NSApplication method mainMenu.

我的委托(delegate)类中有以下内容来获取应用程序的菜单:

- (void)applicationDidFinishLaunching:(NSNotification *)notification{
NSMenu *mainMenu=[[NSApplication sharedApplication] mainMenu];
}

为什么mainMenu为零?我是不是在错误的时间调用了它?将 [[NSApplication sharedApplication] mainMenu] 放入我的 View Controller 的 loadView 中也会返回 nil。

我想做的就是向应用程序菜单栏添加项目。

最佳答案

代码返回 nil,因为关联的 Storyboard 没有应用程序场景/菜单栏。

使用 xcode 创建一个新的 Cocoa 应用程序项目为我构建了这一切,我只需要移植我的文件即可。上面的代码现在按预期返回 NSMenu。

关于macos - 为什么 NSApplication sharedApplication mainMenu 返回 nil ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41831912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com