1

I have an API that allows users to create popup menus with sub menus, and I'm having problems detecting the selected item when the user clicks on an option that belongs to a sub menu.

So, the display and "construction" of the popup menu is correct and it works fine. Starting from a NSMenu I add a few NSMenuItems, then for some NSMenuItems I add a new NSMenu with a few NSMenuItems more.

The problem begins when I click on an item that belongs to a submenu, the selectedItem I get is always nil. It works fine for the main menu. Here's how I show the menu:

NSRect frame = NSMakeRect(mp.origin.x + 10, mp.origin.y + 10, 1, 1);

NSPopUpButtonCell *cell = [[NSPopUpButtonCell alloc] initTextCell: @"" pullsDown: NO];
[cell setAutoenablesItems: NO];
[cell setAltersStateOfSelectedItem: NO];
[cell setMenu: mainMenu];
[cell selectItem: Nil];
[cell performClickWithFrame: frame inView: [window initialFirstResponder]];

NSMenuItem *xpto = [cell selectedItem];
megamau
  • 33
  • 5
  • Cells are usually used within a control. Why don't you use `NSPopUpButton`? – Willeke Jul 05 '16 at 22:26
  • The NSPopUpButton does no allow the use of performClickWithFrame, which is how the menu waits for the user input. Also, instead of a simple menu, with the NSPopUpButton, I get a dropdown box. I also read on a mailing list that this specific usage I want is not possible. So what I'll try next, is to take advantage of the "onItemClick:" event of the NSMenuItem in order to detect what was clicked. – megamau Jul 06 '16 at 14:38
  • If you only want the menu, create a menu and call one of the `popUp…` methods of NSMenu. – Willeke Jul 06 '16 at 21:07

0 Answers0