NSMenuDelegate not called for submenu in NSMenuItem created by another
NSMenuDelegate
I have an NSStatusItem with an NSMenu that has a delegate. This delegate
dynamically updates the menu based on a few factors. So per the
documentation, to quickly update the menu, I use the method:
- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item
atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel;
At the appropriate time in this method I call the following:
NSMenu *submenu = [[NSMenu alloc] init];
SomeSubmenuDelegate *submenuDelegate = [[SomeSubmenuDelegate alloc] init];
submenu.delegate = submenuDelegate;
item.submenu = submenu;
The status menu shows correctly, and the appropriate menu items have
disclosure triangles for submenus; however, the submenus do not appear
when the appropriate items are highlighted, even though in the
SomeSubmenuDelegate the following method exists, which based on my
experience should show three blank menu items:
- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
{
return 3;
}
Logging does nothing as well, not even in menuWillOpen, so it appears the
methods are never called, making me believe that somewhere the delegate is
unset, or the menu is copied somehow during runtime and the delegate unset
in the process...
The workaround for now is to create the entire submenu in the top level
NSMenuDelegate (for the NSStatusItem's menu) instead of assigning a
delegate. There are a few drawbacks here
1) I am keeping my actions within the NSMenuDelegate, and it would be nice
to have separate files for the parent and submenus.
2) While not necessarily material, that means that I am allocating memory
for each submenu NSMenuItem before it is every needed.
It seems optimally, I should be able to use a delegate here just like with
any other NSMenu.
Any thoughts / suggestions are greatly appreciated. First question for me
here!
No comments:
Post a Comment