递归搜索NSMenu中的NSMenuItem [英] Searching NSMenuItem inside NSMenu recursively

查看:157
本文介绍了递归搜索NSMenu中的NSMenuItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法itemWithTitleNSMenu中定位菜单项.但是,它仅在第一级内部显示.我找不到在所有嵌套子菜单中进行搜索可以递归地完成相同工作的现成方法.或者,以某种方式等效地,递归地滑动NSmenu的函数.

The method itemWithTitle locates a menu item within a NSMenu. However it looks only inside the first level. I cannot find a ready-to-use method that will do the same job recursively by searching inside all the nested submenus. Or, somehow equivalently, a function that swipes NSmenu's recursively.

对我来说,这样的事情将不复存在是非常不可思议的.也许有些与NSMenu不直接相关的功能可以派上用场吗?

It looks quite incredible to me that such a thing would not exist. Maybe there is some function not directly related to NSMenu that can come in handy?

推荐答案

好吧,由于某些特定原因(超出了本篇文章的范围,无论如何,我都没有兴趣),我确实需要做这种笨拙的解决方法,所以我结束了我自己编码.

Ok, since I really need to do this clumsy workaround for a number of specific reasons (beyond the scope of this post, and quite uninteresting anyway) I ended up coding it myself.

以下是代码段:

NSMenuItem * mitem;

while (mitem) { // loop over all menu items contained in any submenu, subsubmenu, etc.

     // do something with mitem ....

     mitem = next_menu_item(mitem);
}

由以下功能支持:

NSMenuItem * goto_submenu(NSMenuItem * mitem){

    NSMenu * submen = [mitem submenu];
    if (submen && [[submen title] isNotEqualTo:@""] && [submen numberOfItems])
        return goto_submenu([submen itemAtIndex:0]);

    return mitem;
};

NSMenuItem * next_menu_item(NSMenuItem * mitem){

    NSMenu * menu = [mitem menu];

    if ([menu indexOfItem:mitem]==([menu numberOfItems]-1)) //if is last item in submenu go to parent item
        return [mitem parentItem];

    return goto_submenu([menu itemAtIndex:([menu indexOfItem:mitem]+1)]);
};

这篇关于递归搜索NSMenu中的NSMenuItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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