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

As stated in the document, it is not possible to add two menu items to NSPopUpButton if they both have the same title. I tried to add menu items to the [popupButton menu], but no luck. I also tried to create a new menu, add elements to it, and then use [popupButton setMenu: newMenu], but no. A menu always displays only one item for a name.

But I know that it should be possible if you try to create a smart playlist in iTunes, you can select "Playlist" from the left "=" button from the middle, and in the right - menu items for each playlist in iTunes EVEN, if They have the same title. So how do they do it?

+3
source share
3 answers

I had the exact problem and it was resolved easily. Instead of using NSPopUpButton methods such as -addItemWithTitle: to control button elements, I added NSArrayController and added elements to the array controller. Then I used bindings to bind the controller and popup button, and now it shows elements with the same name.

To complete the bindings:

  • Add NSArrayController to IB.
  • Install NSPopUpButton bindings for the "Content" in the Array Controller by using the Control key " " placed objects "
  • NSPopUpButton " " Array Controller " " "selectionIndex"
  • [] , , . NSString, NSMutableDictionary , , , , , , . , , "" " " . , .
0

NSPopUpButton, addItemWithTitle: addMenu: , , . NSMenuItem.

, (, ), , , , :

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

addItemWithTitle: NSMenuItem . , , .

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

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

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


All Articles