强制更新 Cocoa 应用程序主菜单的 NSMenu(嵌套子菜单)
- 我插入了一些子菜单作为主菜单的窗口项子菜单
- 我有一个从 NSObject 继承的对象实例(假设它的类名称是 MenuController),并支持来自 NSMenuDelegate 方法的 2 个: – 菜单中的项目数量: – menu:updateItem:atIndex:shouldCancel:
- 此实例作为蓝色对象添加到 NIB 中,以便在运行时唤醒
- 步骤 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 会调用我的委托方法 - 我发现,有时它不想调用任何东西。看起来像是随机策略。
如何确保在“某些调用”之后我的子菜单在内部数组修改后将处于实际状态?
- I have some submenu inserted as Window item submenu of Main Menu
- 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: - This instance added as Blue-Object into NIB for awaking at runtime
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要实现 1:1 映射,请在委托中实现以下 3 个方法:
and
和
AttachedMenu - 是 NSMenu* 类型的内部变量
接下来,当您想强制刷新子菜单时,随时 - 只需调用
To implement 1:1 mapping, implement in delegate these 3 methods:
and
and
attachedMenu - is internal var of type NSMenu*
Next, when you wanna force refresh the submenu, anytime - just call
你走在正确的轨道上。这可能是 Cocoa 应用程序中需要使用并行数组的一种情况。
numberOfItemsInMenu:
时,将您拥有的模型对象的数量与菜单项数组的计数进行比较。如果较少,请使用removeObjectsInRange:
方法 来缩短菜单项数组。如果更多,请使用 填充数组NSNull 对象。 (这里不能使用nil
,因为 NSArray 只能包含对象,而nil
表示不存在对象。)menu:updateItem 时:atIndex:shouldCancel:
, 在返回新菜单项之前,用新菜单项替换数组中该索引处的对象。update
方法的文档。在您的验证方法中, 查找数组中菜单项的索引,然后获取模型对象数组中该索引处的模型对象并从中更新菜单项。如果您使用的是 Snow Leopard,则可以向菜单项的菜单发送propertiesToUpdate
消息 确定需要从模型对象授予哪些属性值。需要注意的是,该对象(菜单的委托)也必须是菜单项的目标。我假设是这样。如果不是,这将在步骤 #4 失败,因为验证消息将发送到菜单项的目标。
您可能需要提交增强请求寻求更好的方法。
You're on the right track. This may be the one situation in which a parallel array in a Cocoa app is warranted.
numberOfItemsInMenu:
, compare the number of model objects you have to the count of the menu-items array. If it's fewer, use theremoveObjectsInRange:
method to shorten the menu-items array. If it's more, pad the array out with NSNull objects. (You can't usenil
here, since an NSArray can only contain objects, andnil
is the absence of an object.)menu:updateItem:atIndex:shouldCancel:
, replace the object in the array at that index with the new menu item before returning the new menu item.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 apropertiesToUpdate
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.
在委托调用
numberOfItemsInMenu:
中调用removeAllItems
...这很奇怪,但菜单不会更新,即使它正在调用委托In the delegate call for
numberOfItemsInMenu:
callremoveAllItems
... it's quite odd, but else the menu doesn't update, even it's calling for a delegate