Posted By

0xced on 09/18/07


menu item nsmenu nsmenuitem

Versions (?)

Who likes this?

4 people have marked this snippet as a favorite


Add a menu item

 / Published in: Objective C

Programmatically add a menu item

  1. // Beware: the keyEquivalent must be lowercase in order not to have the shift modifier
  2. NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:@"Action" action:@selector(menuItemAction:) keyEquivalent:@"r"];
  3. [menuItem setTarget:self];
  4. [menuItem setKeyEquivalentModifierMask:NSCommandKeyMask | NSAlternateKeyMask];
  5. // Add the menu item at the end of menu whose index is 1 (most probably the File menu)
  6. [[[[NSApp mainMenu] itemAtIndex:1] submenu] addItem:menuItem];

Report this snippet  

You need to login to post a comment.