views:

775

answers:

2

I have a cocoa app with two types windows each of which requires a different main menu to be displayed.

In my MainMenu.xib I have the default MainMenu. In Window1.xib I have Window1 and in Window2.xib I have Window2 and it's MainMenu.

When I have the first Window open I have the default Menu, when I open Window2 I get it's menu.

However, when I switch back to Window1 I still see Window2's menu. How do I make the menu that is displayed follow the key window?

Thanks

+2  A: 

NSApplication has a method, - (void)setMainMenu:(NSMenu *)aMenu. You can pass it a reference to the correct menu in the appropriate window controller, by implementing - (void)windowDidBecomeKey:(NSNotification *)notification.

Keep in mind it may be easier to change just the submenus instead of swapping out the entire main menu, since you won't have to maintain two different copies of the application, help, and other menus that won't change between the two windows.

Marc Charbonneau
How do I get the menu? Do I need to load it from the Nib each time? You have a point about swapping submenus, maybe that will be easier. I'll look into that.
FigBug
I would create two different NSWindowController subclasses, for Window1.nib and Window2.nib. You can create an IBOutlet instance variable to keep a reference to the menu for each nib. The docs on NSWindowController will explain the design in more detail, but let me know if you have other questions.
Marc Charbonneau
Works greak, thank you very much.
FigBug
Don't do this, it is bad practice -1
Stephan Eggermont
+10  A: 

Generally, you shouldn't replace the entire main menu every time. It's more compliant with the Human Interface Guidelines to simply disable any menu items that don't apply to the current window. And if you really should have a completely different set of menus in the menu bar, maybe you should split that part of your application into a separate application.

Peter Hosey
I thought about this as well, but it ads a lot of complexity of sharing the data between applications. It's kind of like XCode/Interface Builder, should they be separate apps or the same app. You can make arguments both ways.
FigBug