mainMenu

mac app 的菜单创建:新增一个菜单用于显示长链接的连接状态

Posted by kunnan on October 26, 2018

前言

image

code

/**
 https://github.com/TKkk-iOSer/WeChatPlugin-MacOS/blob/731b504bb4639fceb3035547d42f18a33b7f0f9f/WeChatPlugin/Sources/Managers/TKAssistantMenuManager.m#L116
 */
+  (void)setupmainMenu{
    NSString *tmp = @"连接状态";
    NSMenu *subMenu = [[NSMenu alloc] initWithTitle:tmp];
    
    
//    [subMenu addItems:@[preventRevokeItem,
//                        autoReplyItem,
//                        commandItem,
//                        newWeChatItem,
//                        onTopItem,
//                        autoAuthItem,
//                        pluginItem
//                        ]];
//    [subMenu setSubmenu:subPluginMenu forItem:pluginItem];

    NSMenuItem *menuItem = [[NSMenuItem alloc] init];
    
    [menuItem setTitle:tmp];
    
    [menuItem setSubmenu:subMenu];
    //    menuItem.target = self;
    [[[NSApplication sharedApplication] mainMenu] addItem:menuItem];
    
    menuItem.enabled = NO;
}

/**
  新增ws连接状态的显示
 @param tilte <#tilte description#>
 */
+ (void) setupupdatemenuItemTitle:(NSString*)tilte{
    NSLog(@" setupupdatemenuItemTitle json :%@",tilte);
    NSMenu *mainMenu  =  [[NSApplication sharedApplication] mainMenu];
    
//    NSLog(@" setupupdatemenuItemTitle mainMenu title :%@",[[mainMenu itemAtIndex:0] title]);
//    NSLog(@" setupupdatemenuItemTitle mainMenu  submenu title :%@",[[[mainMenu itemAtIndex:0] submenu] title]);

    
    if ([mainMenu numberOfItems] > 0) {
        NSMenuItem *NSMenuItem = [mainMenu itemAtIndex:[mainMenu numberOfItems]-1];
        [NSMenuItem setTitle:tilte];
//        NSLog(@" setupupdatemenuItemTitle NSMenuItem title :%@",[NSMenuItem title]);
        NSMenu *appMenu = [NSMenuItem submenu];// 必须修改这个才会生效
        [appMenu setTitle:tilte];
//        NSLog(@" setupupdatemenuItemTitle submenu title :%@",[appMenu title]);
        [mainMenu update];
    }
    
}


See Also

  • newpost
/Users/devzkn/bin//newpost mainMenu mac app 的菜单创建:新增一个菜单用于显示长链接的连接状态 -t macos
#原来""的参数,需要自己加上""

转载请注明: > mainMenu