How to change the main menu in Cocoa?

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

In my MainMenu.xib, I have the main MainMenu file. In Window1.xib I have Window1 and in Window2.xib I have Window2 and MainMenu.

When I open the first window, I have a default menu, when I open Window2, I get its menu.

However, when I switch back to Window1, I still see the Window2 menu. How to create a displayed menu after the key window?

+4
source share
2 answers

NSApplication has a method - (void)setMainMenu:(NSMenu *)aMenu . You can give it a link to the correct menu in the corresponding window controller by executing - (void)windowDidBecomeKey:(NSNotification *)notification .

Keep in mind that it’s easier to change only the submenu instead of replacing the entire main menu, since you will not need to support two different copies of the application, help and other menus that will not change between the two windows.

+5
source

Generally, you should not replace the entire main menu every time. It is more compatible with the Human Interface Guide to simply disable any menu items that are not relevant to the current window. And if you really should have a completely different set of menus in the menu bar, perhaps you should separate this part of your application into a separate application.

+12
source

All Articles