Search code examples
objective-ccocoansmenunsmenuitem

Can't populate NSMenu from document controller


I've a MainMenu.xib and a MyDocument.xib. I need to dynamically populate some NSMenuItem's from my document controller but I'm at a loss on how to get to it from MainMenu.xib.

I've tried adding an NSObject to MainMenu.xib and make it an instance of my document controller but I can't populate the NSMenuItem's from the NSArrayController I have on the controller.

Should I just do this in code or is there another way of tackling this?


Solution

  • In MyDocument.m:

    - (void)windowControllerDidLoadNib:(NSWindowController *) aController
    

    try this:

    NSMenu * mainMenu = [[NSApplication sharedApplication] mainMenu];
    

    See Introduction to Application Menus and Pop-up Lists in Apple docs for more