11

I want to add a drop down menu in one of the entries in the NSMenu Item. (eg. If you click on the Battery indicator on Finder bar, it has an option for Show->Icon,Time,Percentage). Now I add a MenuItem using the following code:

         menuItem = [menu addItemWithTitle:@"Start"
        action:@selector(start:) keyEquivalent:@""]; 
        [menuItem setTarget:self];

How do I add a submenu Item with this drop down list ? Thanks.

ZionKing
  • 326
  • 1
  • 4
  • 16

2 Answers2

23

This is how I add a submenu to an NSMenu item:

NSMenuItem *mainItem = [[NSMenuItem alloc] init];
[mainItem setTitle:@"Main item"];

NSMenu *submenu = [[NSMenu alloc] init];
[submenu addItemWithTitle:@"Sub item" action:nil keyEquivalent:@""];

[mainItem setSubmenu:submenu];
Francesco Papagno
  • 617
  • 10
  • 29
-3

Got it working. Created a NSPopuButton with contents from an array and then used that here.

[menu setSubmenu:[(NSPopupButton *array) menu] forItem:menuItem];
ZionKing
  • 326
  • 1
  • 4
  • 16
  • 6
    1. That isn't valid syntax. 2. Why did you name your pop-up button “array”? And why are you casting it? 3. You probably shouldn't put the same menu in two different places. Make a copy of the pop-up button's menu and use that as the submenu. Or, if you're not actually using the pop-up button, why are you creating one? – Peter Hosey Aug 18 '10 at 10:51