简体   繁体   English

为什么 NSApplication sharedApplication mainMenu 返回 nil?

[英]why does NSApplication sharedApplication mainMenu return nil?

Apple's docs say:苹果的文档说:

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.菜单栏中的所有应用程序菜单都归应用程序启动时创建的一个 NSMenu 实例所有。 You can retrieve this main menu with the NSApplication method mainMenu.您可以使用 NSApplication 方法 mainMenu 检索此主菜单。

I have the following is in my delegate class to obtain the app's menu:我在我的委托类中有以下内容来获取应用程序的菜单:

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

why is mainMenu nil?为什么mainMenu为零? Am I calling it at the wrong time?我是不是在错误的时间调用它? Putting [[NSApplication sharedApplication] mainMenu] in my view controller's loadView also returns nil.[[NSApplication sharedApplication] mainMenu]放在我的视图控制器的loadView也会返回 nil。

All I want to do is add items to the application menu bar.我想要做的就是将项目添加到应用程序菜单栏中。

The code returns nil because the associated Storyboard does not have an Application Scene/menu bar.代码返回 nil,因为关联的 Storyboard 没有应用程序场景/菜单栏。

Creating a new Cocoa Application project with xcode built all this for me and I just had to port my files over.使用 xcode 创建一个新的 Cocoa 应用程序项目为我构建了所有这些,我只需要移植我的文件。 The code above now returns NSMenu as expected.上面的代码现在按预期返回 NSMenu。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM