why does NSApplication sharedApplication mainMenu return nil?

1k Views Asked by At

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. You can retrieve this main menu with the NSApplication method 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? Am I calling it at the wrong time? Putting [[NSApplication sharedApplication] mainMenu] in my view controller's loadView also returns nil.

All I want to do is add items to the application menu bar.

1

There are 1 best solutions below

0
On

The code returns nil because the associated Storyboard does not have an Application Scene/menu bar.

Creating a new Cocoa Application project with xcode built all this for me and I just had to port my files over. The code above now returns NSMenu as expected.