Sharing NSMenuItems across multiple NSMenus
I've got multiple menus in the same application and would like to share submenus between them.
For simplicity's sake, let's say one is the main menu, the other the dock tile menu..
I've got submenus that contain quite a few items (let's say font names), many of which I need to keep an iboutlet reference to for various reasons.
In IB I can't find any way of "sharing" those submenus, so I end up 开发者_如何转开发duplicating the IB definitons (ie. re-creating by hand each item in two different places), keeping references to two nsmenuitems rather than just the one, etc.. it's not nice.
Is there any way of sharing sub menu definitions between multiple NSMenus?
I've tried to create a top-level menu item in IB but then I can't seem to link it to anything in the menu definitions. Besides I'm worried that even if I manage to do this programatically, it might mess up bindings, responder hierarchies, memory management, etc.
Is there a best practice for doing this? has anybody done it? and what were the results?
If the same controller is the target of all the menu items, set it as their delegate, and have it implement the NSMenuDelegate protocol—specifically, the numberOfItemsInMenu:
and menu:updateItem:atIndex:shouldCancel:
methods. Each menu will ask you how many items it should have, create as many items as necessary to fill the deficit, and then ask you to fill them out (including setting their actions and possibly targets).
The result is one place in your code defining both menus.
精彩评论