4

How can I create an NSPopUpButton programmatically and attach the menu items to it? This is what I have so far but it is not click able nor does it have any menu items attached

help window is just the name of my NSWindow

NSPopUpButton *button = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(10, 0, 50, 50)];
[[helpWindow contentView] addSubview:button];
[button setNeedsDisplay:YES]; 
pkamb
  • 33,281
  • 23
  • 160
  • 191
Grant Wilkinson
  • 1,088
  • 1
  • 13
  • 38

3 Answers3

9

You can also create an NSMenuItem then add it to the NSPopUpButton's menu:

NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"menu" action:NULL keyEquivalent:@""];

[[popUpButton menu] addItem:menuItem];
pkamb
  • 33,281
  • 23
  • 160
  • 191
6

Use the designated initializer initWithFrame:pullsDown:, and then use addItemWithTitle: or addItemsWithTitles: to add the menu items

pkamb
  • 33,281
  • 23
  • 160
  • 191
rdelmar
  • 103,982
  • 12
  • 207
  • 218
1

a simple one-liner does the trick for me:

[[popUpButton menu] addItemWithTitle:@"some title string" action:NULL keyEquivalent:@""];
petermafia
  • 44
  • 3