按标题获取NSMenuItem的NSMenuItem [英] Getting NSMenuItem of NSMenu tree by title

查看:316
本文介绍了按标题获取NSMenuItem的NSMenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSMenu (让我们说主菜单),其中有很多 NSMenu NSMenuItem 在不同的级别。



我想要能够获得NSMenuItem实例指定树路径



>

菜单:




  • 文件

    • li>
    • 打开

      • 文档

      • 项目

      / li>
    • 保存

    • 另存为...




路径:/ File / Open / Document


解决方案

我认为最好的方法是:通过指定其标题或更好地定义自定义标签来获取 NSMenuItem

  #define kMenuFileNew 1 
#define kMenuFileOpen 2

NSMenu * menu = [[NSMenu alloc] initWithTitle:@File];
NSMenuItem * item1 = [[NSMenuItem alloc] initWith ..];
item1.tag = kMenuFileOpen;
[menu addItem:item1];


NSMenuItem * item2 = [menu itemWithTag:kMenuFileOpen];


I have an NSMenu (let's say the Main Menu), with lots of NSMenus in it, and NSMenuItems at different levels.

I want to be able to get the NSMenuItem instance specifying the tree path (with the title of the corresponding NSMenus/NSMenuItems in it).

Example :

Menu :

  • File
    • New
    • Open
      • Document
      • Project
    • Save
    • Save As...

Path : /File/Open/Document

How would you go about it, in the most efficient and Cocoa-friendly way?

解决方案

I think that best way would be to obtain the NSMenuItem by specifying its title or, better, a custom defined tag.

#define kMenuFileNew 1
#define kMenuFileOpen 2

NSMenu *menu = [[NSMenu alloc] initWithTitle:@"File"];
NSMenuItem *item1 = [[NSMenuItem alloc] initWith..];
item1.tag = kMenuFileOpen;
[menu addItem:item1];


NSMenuItem* item2 = [menu itemWithTag:kMenuFileOpen];

这篇关于按标题获取NSMenuItem的NSMenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆