2

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
dev4life
  • 10,785
  • 6
  • 60
  • 73
  • 1
    What have you tried to debug this? Did you create a new project and tried you code there? It's working for me from a view controller (haven't tested applicationDidFinishLoading but it should work too. – HAS Jan 24 '17 at 16:06
  • 1
    @HAS you are right, it works for new Cocoa Application projects. I have moved my files to the new project! – dev4life Jan 24 '17 at 20:30
  • glad it helped :) If you have time and find the issue please answer your own question (and maybe file a bug report at bugreport.apple.com). Thanks! – HAS Jan 24 '17 at 20:31

1 Answers1

1

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.

dev4life
  • 10,785
  • 6
  • 60
  • 73