Re: A problem with PopupButtonCell --- submenu selection not available
Re: A problem with PopupButtonCell --- submenu selection not available
- Subject: Re: A problem with PopupButtonCell --- submenu selection not available
- From: Graham Cox <email@hidden>
- Date: Tue, 6 Jul 2010 10:02:47 +1000
On 06/07/2010, at 12:31 AM, Motti Shneor wrote:
> One catch though... In my situation, the menus, sub-menus and items are prepared in advance, somewhere-else-in-the-code, where I don't yet know the target and action.
> These are only available for me when I'm actually activating the menu.
>
> Oh yeah I can recursively scan the menu and set up the action and target on each item, just before activating the menu --- but this is SO ugly.
I agree, but you have to do it somewhere. That's why I suggested a delegate which at least is given a chance to populate each menu item in turn so at least the iteration part is done for you as well as some optimizations to reuse menu items. Of course that requires that the delegate knows the target/action (easiest if the delegate is the target). It can end up fairly elegant, in that creating the menu is no work at all (you don't need to add any items at that point), and the delegate just waits to be called with -numberOfItemsInMenu: and -menu:updateItem:... at which point the entire content is fleshed out, one item at a time (this is surprisingly fast). Whenever you get to an item that has a submenu, you just repeat the process - add an empty menu and sets its delegate to your menu handler, then when the submenu is shown it will get populated in the identical fashion. This is more optimum in that you only ever populate menus that are actually displayed - anything the user doesn't open is untouched.
Your situation sounds similar to one of mine: in my app I have a controller which manages menus like this, so it is the menu's delegate (it dynamically repopulates the menus as the data model the menus represent changes), but it is acting on behalf of various other targets (who just need to know when and what was chosen in the menu). It therefore needs to maintain a mapping between any NSMenu it is managing and the target/action it needs to apply to each item. I had to set up a small subsystem to handle this myself - there was nothing really built-in to do it (apart from dictionaries, etc).
> I was wondering if there is something on the NSMenu itself I could use to dictate a single target and action for all its menu items?
> I saw a mysterious "submenuAction" category property accessor --- can it be of use here? What is the purpose of an NSMenu action?
That appears to be an implementation detail internal to NSMenu used when handling a submenu. I'm not exactly sure what it does and have never used it. Note there's no -setSubmenuAction: corresponding to it. It seems to be an override point only.
It would be nice if you could set a target/action for the entire menu such that if no item has a target/action set, it uses the menu's. But there isn't at the moment, so you have to do it yourself, and file enhancement requests.
Also, as others have mentioned, NSOutlineView might be an appropriate alternative. In my case it would take up too much space in a floating palette which is why I use a menu.
--Graham
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden