Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple menu items with the same title to NSPopUpButton(NSMenu)?

As docs say it's impossible to add two menu items to NSPopUpButton if they both have the same title. I was trying to add menu items to [popupButton menu], but with no luck. I was also trying to create a new menu, add items to it and then use [popupButton setMenu:newMenu], but no. Menu always display only one item per name.

But I know it should be possible, if you try to create a smart playlist in iTunes, you could select "Playlist" from the left popup button, "=" from the middle, and the right one will hold menu items for every playlist in iTunes EVEN if they have the same title. So how do they do it?

like image 436
William S. Pear Avatar asked Feb 23 '10 16:02

William S. Pear


1 Answers

While NSPopUpButton methods like addItemWithTitle: and addMenu: won't allow duplicate names, it is definitely possible to have items with the same title. You simply have to set the name on the NSMenuItem itself.

For example, if you have an array of strings (like playlist names perhaps) you want to add them to a popup button, and want to make sure duplicates will be in there, do it like this:

NSArray* items = [NSArray arrayWithObjects:@"Foo", @"Bar", @"Baz", @"Foo", nil];

for (NSString* item in items)
{
   [popupButton addItemWithTitle:@"blah"];
   [[popupButton lastItem] setTitle:item];
   [[popupButton lastItem] setTarget:self];
   [[popupButton lastItem] setAction:@selector(something:)];
}
like image 103
Ken Aspeslagh Avatar answered Sep 30 '22 10:09

Ken Aspeslagh