macOS - Main Menu
2018-07-06 本文已影响17人
VictorZhangWang
添加子menu
1.新加menu按钮
添加menu
2.新增的子menu是disabled(Test是后面改的)
menu状态-
使Test menu有效
为Test menu 添加方法
image.png -
重写validateMenuItem: 方法,方法返回值决定menu是否有效,默认返回YES(有效的)
- (void)testAction:(id)sender
{
NSLog(@"--------");
}
-(BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if ([menuItem.title isEqualToString:@"Test"])
{
return YES;
}
return NO;
}
代码为Main Menu添加item 和快捷键
NSMenu* mainMenu = [[NSApplication sharedApplication] mainMenu];
NSMenu* toolsMenu = [[mainMenu itemWithTitle:@"Tools"] submenu];
NSMenu* externalEditorsMenu = [[toolsMenu itemWithTitle:@"External Editors"] submenu];
if (externalEditorsMenu)
{
[externalEditorsMenu removeAllItems];
NSMenuItem *defaultItem = [[NSMenuItem alloc]initWithTitle:@"Default External Editor" action:@selector(defaultExternalEditor:) keyEquivalent:@"k"];
defaultItem.keyEquivalentModifierMask = NSEventModifierFlagControl | NSEventModifierFlagOption;
[externalEditorsMenu addItem:defaultItem];
[externalEditorsMenu addItem:[NSMenuItem separatorItem]];
}