Cocoa - 以编程方式添加菜单项

时间:2010-07-10 13:22:03

标签: cocoa interface-builder menu

如何以编程方式在子菜单中添加菜单项?

2 个答案:

答案 0 :(得分:7)

使用-[NSMenu addItem:]等,例如:

NSMenu *subMenu = ...;
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"foo" 
                    action:@selector(fooClicked:) keyEquivalent:@""]; 
[item autorelease];
[item setTarget:self];
[subMenu addItem:item];

答案 1 :(得分:1)

雨燕5

let subMenu = NSMenu()
let menuItem = NSMenuItem(
    title: "Menu item title",
    action: #selector(menuItemClicked(_:)),
    keyEquivalent: ""
)
menuItem.target = self
menuItem.toolTip = "Menu item tooltip"
subMenu.addItem(menuItem)