美文网首页
macOS NSMenu

macOS NSMenu

作者: devileatapple | 来源:发表于2020-10-16 15:26 被阅读0次

    macOS相对于iOS来说,它的APP多了一个顶部的目录栏。这个目录栏的配置就有意思了。直接上代码

        /**

            NSMenu->单个NSMenuItem

            单个NSMenuItem先绑定对应的NSMenu对象(setSubmenu),在NSMenu对象中创建对应的子NSMenuItem(addItemWithTitle:/addItem:)

            显示的其实都是menuitem,其中有对应action或者有子menuitem的item才可以点击,否则就是置灰状态

         */

        NSMenu*mainMenu=[NSAppmain Menu];//获取默认的主目录

        ///添加一级menu keyEquivalent 快捷键win+字符串字符,如果有相同的字符,后续的快捷键会变成shift+win+字符

        NSMenuItem *userItem=[[NSMenuItem alloc]initWithTitle:@"一级目录" action:nil keyEquivalent:@"U"];

        [userItem setTitle:@"User"];

        [mainMenu insertItem:userItem atIndex:2];//插入item设定对应下标位置

        ///添加二级目录

        NSMenu *subMenu=[[NSMenu alloc]initWithTitle:@"User"];

        [userItem setSubmenu:subMenu];//设置子目录

        [subMenu addItemWithTitle:@"name" action:@selector(load1) keyEquivalent:@"N"];

        [subMenu addItemWithTitle:@"age" action:@selector(load2) keyEquivalent:@"A"];

        NSMenuItem *genderItem=[[NSMenuItem alloc]initWithTitle:@"Gender" action:nil keyEquivalent:@"G"];

        [subMenu addItem:genderItem];

        ///添加三级目录

        NSMenu *genderSubMenu=[[NSMenu alloc] initWithTitle:@"gender"];

        [genderItem setSubmenu:genderSubMenu];

        NSMenuItem *boyItem=[[NSMenuItem alloc]initWithTitle:@"boy" action:@selector(load1) keyEquivalent:@"B"];

        NSMenuItem *girlItem=[[NSMenuItem alloc]initWithTitle:@"girl" action:@selector(load2) keyEquivalent:@"G"];

        [genderSubMenu addItem:boyItem];

        [genderSubMenu addItem:girlItem ];

        //重置目录

        [NSApp setMainMenu:mainMenu];

    相关文章

      网友评论

          本文标题:macOS NSMenu

          本文链接:https://www.haomeiwen.com/subject/rvizpktx.html