Mark A. Donohoe
Mark A. Donohoe

Reputation: 30458

How can you specify which NSMenu is the main menu in a XIB file?

Long time iOS developer who's dipping his toes in the macOS world. Just trying to better understand how Mac apps work.

For non-storyboard applications based on a XIB, the template creates a default xib and inside is the application's main menu.

However, if I delete that XIB, then create a new one, how do I set up a menu to be the application's main menu? It seems to ignore whatever I create. Plus, when I add one, the graphic looks different than the original; here it's stacked vertically whereas the original had them stacked horizontally.

So, how can you create a second xib (in case you deleted the first one) and replace the main menu in it?

Upvotes: 3

Views: 854

Answers (1)

clemens
clemens

Reputation: 17712

You have to do two steps to change the main menu to a menu in another XIB file:

  1. Change the Main Interface in Deployment Info in the General tab of the Target of your application to the new XIB file. enter image description here
  2. Open the source code of the new XIB file and search for your new Main Menu. This is a XML tag named menu. Add an attribute named systemMenu with the value main to it. This should look like:

    <menu title="Second Menu" systemMenu="main" id="usV-GH-tFG" userLabel="New Main Menu">
    

Now, your app should use the new Main Menu as System Menu. Some submenus should also have systemMenu attributes with the values apple, services, recentDocuments, font or window. If you have made this change, the display in the Interface Builder also changes.

Building a new menu takes a little effort because you have to create quite a few submenus and entries.

But this way you can easily make mistakes, so you should better create a new Main Menu by copying an existing one.

Upvotes: 3

Related Questions