强制更新 Cocoa 应用程序主菜单的 NSMenu(嵌套子菜单)

发布于 2024-08-21 00:39:34 字数 1073 浏览 9 评论 0原文

  1. 我插入了一些子菜单作为主菜单的窗口项子菜单
  2. 我有一个从 NSObject 继承的对象实例(假设它的类名称是 MenuController),并支持来自 NSMenuDelegate 方法的 2 个: – 菜单中的项目数量: – menu:updateItem:atIndex:shouldCancel:
  3. 此实例作为蓝色对象添加到 NIB 中,以便在运行时唤醒
  4. 步骤 2-3 中的对象,配置为子菜单的委托(步骤 1)

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

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

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

因此,为了调用某些项目,我不想去 Window / MySubmenu / MyItem 单击通过鼠标,我想只按一个快捷键,例如 CMD-3 来调用该项目。

好的,定期它会按预期工作。但是,通常,我无法通知主菜单有关嵌套子菜单的更改,除非打开 Window / MySubmenu 重新加载其内容。 重现问题的一种稳定方法 - 只需尝试删除某些项目并按分配给它的旧快捷方式,在创建新项目作为已删除的替换后 - 宾果游戏 - 在导航到 Window / MySubmenu 查看当前子菜单内容之前,快捷方式将不起作用。

我不知道如何强制主菜单重建其子菜单...... 我尝试过: [[NSApp mainMenu] update] 和使用 NSNotificationCenter 发送 NSMenuDidAddItemNotification、NSMenuDidRemoveItemNotification、NSMenuDidChangeItemNotification 的游戏

我尝试出口到我的子菜单并显式调用更新方法 - 没有办法... 有时 AppKit 会调用我的委托方法 - 我发现,有时它不想调用任何东西。看起来像是随机策略。

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

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

居里长安 2024-08-28 00:39:34

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

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

and

- (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* 类型的内部变量

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

[self menuNeedsUpdate:nil];

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];
來不及說愛妳 2024-08-28 00:39:34

我尝试过:[[NSApp mainMenu]更新]...

你走在正确的轨道上。这可能是 Cocoa 应用程序中需要使用并行数组的一种情况。

  1. 保留一个可变的菜单项数组,与菜单项代表的模型对象数组平行。
  2. 当您收到 numberOfItemsInMenu: 时,将您拥有的模型对象的数量与菜单项数组的计数进行比较。如果较少,请使用 removeObjectsInRange: 方法 来缩短菜单项数组。如果更多,请使用 填充数组NSNull 对象。 (这里不能使用 nil ,因为 NSArray 只能包含对象,而 nil 表示不存在对象。)
  3. 当您收到 menu:updateItem 时:atIndex:shouldCancel:, 在返回新菜单项之前,用新菜单项替换数组中该索引处的对象
  4. 符合 NSMenuValidation 协议,如 update 方法的文档。在您的验证方法中, 查找数组中菜单项的索引,然后获取模型对象数组中该索引处的模型对象并从中更新菜单项。如果您使用的是 Snow Leopard,则可以向菜单项的菜单发送 propertiesToUpdate 消息 确定需要从模型对象授予哪些属性值。

需要注意的是,该对象(菜单的委托)也必须是菜单项的目标。我假设是这样。如果不是,这将在步骤 #4 失败,因为验证消息将发送到菜单项的目标。

您可能需要提交增强请求寻求更好的方法。

I tried: [[NSApp mainMenu] update] …

You're on the right track. This may be the one situation in which a parallel array in a Cocoa app is warranted.

  1. Keep a mutable array of menu items, parallel to your array of model objects that the menu items represent.
  2. When you receive numberOfItemsInMenu:, compare the number of model objects you have to the count of the menu-items array. If it's fewer, use the removeObjectsInRange: method to shorten the menu-items array. If it's more, pad the array out with NSNull objects. (You can't use nil here, since an NSArray can only contain objects, and nil is the absence of an object.)
  3. When you receive menu:updateItem:atIndex:shouldCancel:, replace the object in the array at that index with the new menu item before returning the new menu item.
  4. Conform to the NSMenuValidation protocol, as mentioned in the documentation for the update method. In your validation method, find the index of the menu item within the array, then get the model object at that index in the model objects array and update the menu item from it. If you're on Snow Leopard, you can send the menu item's menu a propertiesToUpdate message to determine what property values you need to confer from the model object.

The caveat is that this object, the delegate of the menu, must also be the target of the menu items. I'm assuming that it is. If it isn't, this will fail at step #4, as the validation messages are sent to the menu items' targets.

You may want to file an enhancement request asking for a better way.

闻呓 2024-08-28 00:39:34

在委托调用 numberOfItemsInMenu: 中调用 removeAllItems...这很奇怪,但菜单不会更新,即使它正在调用委托

- (NSInteger)numberOfItemsInMenu:(NSMenu*)menu {

    [menu removeAllItems];

    return [self.templateURLs count] + 2;

}

In the delegate call for numberOfItemsInMenu: call removeAllItems... it's quite odd, but else the menu doesn't update, even it's calling for a delegate

- (NSInteger)numberOfItemsInMenu:(NSMenu*)menu {

    [menu removeAllItems];

    return [self.templateURLs count] + 2;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文