William Jockusch
William Jockusch

Reputation: 27335

programmatically created NSMenu is only showing the first item

I am writing a Mac app with minimal IB usage -- see programatically create initial window of cocoa app (OS X).

Here is my code (via Xamarin/C#):

   public override void DidFinishLaunching(NSNotification notification) {
      // create and show a window, then . . .
      NSMenu fileMenu = new NSMenu();
      NSMenu appMenu = new NSMenu();

      // add some items to both menus, then . . .

      NSMenuItem file = new NSMenuItem("file");
      file.Submenu = fileMenu;

      NSMenuItem app = new NSMenuItem("app");
      app.Submenu = appMenu;

      NSMenu topLevelMenu = new NSMenu();
      topLevelMenu.AddItem(app); // these two lines in 
      topLevelMenu.AddItem(file); // either order

      NSApplication.SharedApplication.Menu = topLevelMenu; // incidentally, setting the MainMenu doesn't appear to do anything
    }
  }

The weird thing is that only the first menu item I add shows up, and its title is changed to the name of my app. So for the above code, I would see the app menu, only, with a title of MyAppName, but the correct items. If I were to instead add the file NSMenuItem first, I would see that menu, again with its name changed to my app name, and the view menu would not show up at all.

I don't mind the changed title; I suppose Apple wants to always show the name of the active app. But it bugs me that I can't get the additional menus to appear.

How can I get all of my menus to show up across the top of the screen?

Upvotes: 2

Views: 1242

Answers (1)

William Jockusch
William Jockusch

Reputation: 27335

I just needed to add titles to my menus:

fileMenu.Title = "file";
appMenu.Title = "whatever";

The title of the first one is still changed to the name of the app.

Upvotes: 2

Related Questions