Question

I'm getting an NSMenuItem from the Main Menu, with the code here : Getting NSMenuItem of NSMenu tree by title

However, something strange happens :

  • An NSMenuItem connected with an action : When using the sender property (NSMenuItem) and setting the title, it works.
  • BUT : When getting the item with the function above and set the title, the NSMenuItem's title does change, but the change is NOT reflected in the menu it belongs to.

What am I doing wrong? (I'm sure this one is really stupid... )

NSMenuItem* mi = [[core mainMenu] getItemWithPath:@"View" tag:PP_MENU_TAG_STATUSBAR];
[mi setTitle:@"newTitle"];
NSLog(@"mi : %@",[mi title]);

// mi changes, but no changes take effect in the mainMenu
Was it helpful?

Solution

I would forget the Getting NSMenuItem of NSMenu tree by title code and just connect each menu in IB.

Then use the setTitle on it when needed

UPDATE*

(see comments) It took me awhile to figure out why even my test one was not working!!. I had put in a attributed title within IB.

enter image description here

So when I later used setTitle. The property was being set but the actual displayed menu was overridden by the attributed title.

Removing the attributed title from IB. fixed this. And setTitle works as expected.

Also I have never used attributed title before. And I just pasted in some formatted coloured text in the IB attributed title. And the menu item was the same in colour and font.

Which I have always wanted to be able to do but thought was not possible.

And doing it programmatically is easy.

 NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"newTestMenu"];
    [string addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0,string.length)];

    [_testMenu setAttributedTitle:string];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top