开发者

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开发者_JAVA技巧 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?


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:)];
}


Instead of using addItemWithTitle:, you can create an NSMenuItem manually and add it directly to the menu. This allows you to specify any title you want, as well as being able to insert it at any location in the menu.

NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:@"foo" action:@selector(something:) keyEquivalent:@""];

[newItem setTarget:self];
[[popupButton menu] addItem:newItem];
[newItem release];


I had the exact problem and it was solved easily. Instead of using NSPopUpButton methods such as –addItemWithTitle: to manipulate the button items, I added an NSArrayController and added the items into the array controller instead. Then I used the bindings to bind the controller and the popup button and now it shows items with same titles.

To do the bindings:

  1. Add an NSArrayController in IB.
  2. Set the NSPopUpButton bindings for "Content" to Array Controller with the "Controller Key" being "arrangedObjects"
  3. Set the NSPopUpButton bindings for "Selected Index" to Array Controller with the "Controller Key" being "selectionIndex"
  4. [Optional] Select the array controller and set the Class Name in attributes to whatever class your items are e.g. NSString or you can use the default NSMutableDictionary and add keys in the box below which consequently lets you wrap your items in a dictionary and add different keys for what you want to show in popup button and what you want to have in background. To set which key of the dictionary you want to be reflected in the popup button, go to popup button's bindings for "Content" again and set the "Modal key Path" to the key you added in the array controller attribute.


Swift 5 Add using guaranteed unique title, then alter title in next statement.

  • "accountPU" is NSPopUpButton object
  • "from" is an account object.
    accountPU.addItem(withTitle: "\(from.accountID!)")      
    accountPU.lastItem!.title = from.name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜