答案 0 :(得分:1)
我猜是,您正在搜索子菜单项。通过搜索子菜单在Interface Builder中找到它。
在所需菜单中插入后,您会找到一个名为Item的新条目。只需编辑这些,或者根据需要填写其他菜单项。这会根据您的要求创建一个菜单结构!
我为您提供了问题的解决方案。根据Apple Docs Context,菜单位于它们所属的视图中。从viewController`viewDidLoad调用该方法。选择器方法根据需要创建。
*编辑*
将创建的菜单分配给主菜单。我是用viewDidLoad
方法完成的。如果你不喜欢在主菜单上,请在主菜单评论后跳过两行。 BTW索引是菜单插入主菜单的位置。
- (void)viewDidLoad {
[super viewDidLoad];
[[self view] setMenu:[self contextMenu]];
NSMenuItem *subMenu = [[NSMenuItem alloc] init];
subMenu.submenu = [self contextMenu];
// Add the menu in mainmenu as well
NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
[mainMenu insertItem:subMenu atIndex:3];
}
-(NSMenu *)contextMenu {
NSMenu *contextMenu = [[NSMenu alloc] initWithTitle:@"Contextmenu"];
NSMenuItem *subMenuItem = [[NSMenuItem alloc] init];
[subMenuItem setEnabled:YES];
[subMenuItem setTarget:self];
[subMenuItem setEnabled:YES];
NSMenu *subMenu = [[NSMenu alloc] initWithTitle:@"Submenu"];
subMenuItem.submenu = subMenu;
[subMenuItem setTitle:[subMenu title]];
// Creates the menu entries.
NSMenuItem *menuItem1 = [[NSMenuItem alloc] initWithTitle:@"otherMenu" action:@selector(subMenuAction:) keyEquivalent:@""];
NSMenuItem *menuItem2 = [[NSMenuItem alloc] initWithTitle:@"anotherMenu" action:@selector(subMenuAction:) keyEquivalent:@""];
// Creates the separator.
NSMenuItem *separator = [NSMenuItem separatorItem];
[subMenu addItem:menuItem1];
[subMenu addItem:separator];
[subMenu addItem:menuItem2];
[contextMenu addItem:subMenuItem];
return contextMenu;
}
- (void)subMenuAction:(id)sender {
}
希望它有所帮助。如果是,请使用绿色回答菜单: - )