Set item names in the main menu of my application?

I am trying to change the names of some items in the main menu of a Cocoa application. I tried to install them both inside IB and programmatically from my applicationDidFinishLaunchingWithOptions: method application. In any case, the title property of my NSMenuItem object changes. But none of my changes are reflected in the actual title of the item at the top of the screen when the application starts.

Can anyone explain what is happening? And how can I change that?

EDIT: The default data structure is set to IB:

NSApplication *app = [NSApplication sharedApplication]; NSMenu *mainMenu = [app mainMenu]; NSArray *itemArray = [mainMenu itemArray]; NSMenuItem *firstItem = [itemArray objectAtIndex: 0]; NSMenu *submenu = [firstItem submenu]; 

I changed the title properties of both firstItem and subMenu to be my desired title. However, the default is still displayed.

+4
source share
3 answers

Rename a project. Seems too extreme. I will still be interested in a less extreme solution.

0
source

Instead of renaming the project change "Bundle name" in your application-Info.plist from $ {PRODUCT_NAME}; to what you want; this change will be reflected in the name of the menu item "Application" of the main menu.

+6
source

Sometimes this just doesn't work, because Cocoa doesn't like your title: -p This happens, for example. when the name you choose is the localized name of the application, but it wants to display the non-localized name of the application. A little trick can help ...

 NSMenu *menu = [[[NSApp mainMenu] itemAtIndex:0] submenu]; NSString *title = @"My app name"; // Append some invisible character to title :) title = [title stringByAppendingString:@"\x1b"]; [menu setTitle:title]; 

Yes, this is crazy, but this extra character eventually changed everything. (You can also add just SPACE, but then the menu item grows, which is probably not the way you want.)

Tested on OS X 10.9.5.

Other:

You must do all this AFTER you have shown the window. Otherwise, it just won't work. In addition, if you perform this procedure when you start the application, and then later, when a window appears, do it again, this may not work.

+4
source

Source: https://habr.com/ru/post/1339305/


All Articles