强制NSMenu(嵌套子菜单)更新Cocoa应用程序的主菜单 [英] Force NSMenu (nested submenu) update for Main Menu of Cocoa App

查看:851
本文介绍了强制NSMenu(嵌套子菜单)更新Cocoa应用程序的主菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我有一些子菜单作为主菜单的窗口项子菜单插入

  2. 我有一个对象的实例(假设它的类名称是MenuController)继承从NSObject并支持来自NSMenuDelegate方法的2:
    - numberOfItemsInMenu:
    - menu:updateItem:atIndex:shouldCancel:

  3. 此实例作为Blue-Object添加到NIB在执行时醒来

  4. 来自配置为子菜单(步骤1)的代理的步骤2-3的对象

现在,我可以在运行时提供子菜单内容。



接下来,我可以添加新项目或从数组中删除旧(在MenuController包含菜单标题)通过协议和委托映射到真正的子菜单。
所有的工作都很好。
除了一件事:我喜欢为我的动态菜单项分配快捷方式。
CMD-1,CMD-2,CMD-3,...



窗口/ MySubmenu / MyItem1 CMD-1,MyItem2 CMD-2,...



所以,对于调用一些项目,我不想去窗口/ MySubmenu / MyItem来点击它的鼠标,我想按一个快捷方式,如CMD-3



好,周期性地按预期工作。但是,一般来说,我没有办法通知主菜单我的嵌套子菜单更改,除了打开Window / MySubmenu重新加载其内容。
一种稳定的方式来重现问题 - 只是尝试删除一些项目,并按下其旧的快捷方式分配给它,在您创建新项目替换删除 - 宾果 - 快捷方式之后,您导航到窗口/ MySubmenu到看到当前的子菜单内容。



我不知道一种强制主菜单重建其子菜单的方法...
我试过:[[NSApp mainMenu]更新]和游戏与NSNotificationCenter发送NSMenuDidAddItemNotification,NSMenuDidRemoveItemNotification,NSMenuDidChangeItemNotification



我试过outlet到我的子菜单和调用更新方法显式 - 没有办法...
有时候,AppKit调用我的委托方法 - 我看到,有时它不想调用任何东西。看起来像一个随机策略。



如何确保在一些调用之后,我的子菜单将在内部数组修改后处于实际状态?

解决方案

要实现1:1映射,请在委托中实现这3个方法:

   - (BOOL)菜单:(NSMenu *)菜单
updateItem:(NSMenuItem *)item
atIndex:(NSInteger)indexCasel:(BOOL)shouldCancel

  (NSInteger)numberOfItemsInMenu:(NSMenu *)menu 

   - (void)menuNeedsUpdate:(NSMenu *)menu 
{
if(!attachedMenu)
attachedMenu = menu;
if(!menu)
menu = attachedMenu;
NSInteger count = [self numberOfItemsInMenu:menu];
while([menu numberOfItems]< count)
[menu insertItem:[[NSMenuItem new] autorelease] atIndex:0];
while([menu numberOfItems]> count)
[menu removeItemAtIndex:0];
for(NSInteger index = 0; index< count; index ++)
[self menu:menu updateItem:[menu itemAtIndex:index] atIndex:index shouldCancel:NO];
}

attachedMenu - 是NSMenu类型的内部var



接下来,当你想强制刷新子菜单,随时 - 只需调用

  [self menuNeedsUpdate:零]; 


  1. I have some submenu inserted as Window item submenu of Main Menu
  2. I have an instance of my object (let's assume its class name is MenuController) inherited from NSObject and supports 2 from NSMenuDelegate methods: – numberOfItemsInMenu: – menu:updateItem:atIndex:shouldCancel:
  3. This instance added as Blue-Object into NIB for awaking at runtime
  4. Object from steps 2-3 configured as delegate for submenu (step 1)

Now, I can provide submenu content in runtime.

Next, I do following: I can add new items or remove old from an array (inside MenuController which contains menu titles) that mapped to real submenu via protocol and delegate. All works just fine. Except one thing: I like assign shortcuts to my dynamic menu items. CMD-1, CMD-2, CMD-3, etc

Window / MySubmenu / MyItem1 CMD-1, MyItem2 CMD-2, ...

So, for call some items I don't wanna go to Window / MySubmenu / MyItem to click it by mouse, I wanna press just one shortcut, like CMD-3 to call the item.

Ok, periodically it works as expected. But, generally, I have no way to inform Main Menu about my nested submenu changes, except open the Window / MySubmenu to reload its content. One stable way to reproduce the issue - just try to remove some item and press its old shortcut assigned to it, after you create new item as replace for deleted - bingo - shortcut wont work before you navigate to Window / MySubmenu to see current submenu content.

I don't know a way to force main menu to rebuild its submenus... I tried: [[NSApp mainMenu] update] and games with NSNotificationCenter for send NSMenuDidAddItemNotification, NSMenuDidRemoveItemNotification, NSMenuDidChangeItemNotification

I tried outlet to my submenu and call to update method explicitly - there is no way... Some times AppKit calls my delegate methods - and I see that, sometimes it doesn't want to call anything. Looks like a random strategy.

How can I make sure that after "some call" my submenu will be in actual state after internal array modifications?

解决方案

To implement 1:1 mapping, implement in delegate these 3 methods:

- (BOOL)menu:(NSMenu *)menu
updateItem:(NSMenuItem *)item 
atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel

and

- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu

and

- (void)menuNeedsUpdate:(NSMenu *)menu
{
    if (!attachedMenu)
        attachedMenu = menu;
    if (!menu)
        menu = attachedMenu;
    NSInteger count = [self numberOfItemsInMenu:menu];
    while ([menu numberOfItems] < count)
        [menu insertItem:[[NSMenuItem new] autorelease] atIndex:0];
    while ([menu numberOfItems] > count)
        [menu removeItemAtIndex:0];
    for (NSInteger index = 0; index < count; index++)
        [self menu:menu updateItem:[menu itemAtIndex:index] atIndex:index shouldCancel:NO];
}

attachedMenu - is internal var of type NSMenu*

Next, when you wanna force refresh the submenu, anytime - just call

[self menuNeedsUpdate:nil];

这篇关于强制NSMenu(嵌套子菜单)更新Cocoa应用程序的主菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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