Roland Rabien
Roland Rabien

Reputation: 8836

How do I change the main menu in Cocoa?

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?

Upvotes: 4

Views: 5254

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96393

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.

Upvotes: 12

Marc Charbonneau
Marc Charbonneau

Reputation: 40513

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.

Upvotes: 5

Related Questions