views:

77

answers:

1

I have an NSMenu which contains both static and dynamically created NSMenuItem's (static meaning NSMenuItem's created in Interface Builder, dynamic meaning NSMenuItem's created at run-time). Although I'm developing on 10.6, my application also offers 10.5 support.

My menu consists of a number of dynamic NSMenuItem's which contain submenus. Currently, I'm using NSMenuItem's parentItem: method (exclusive to 10.6) to grab the parent menu item when a submenu item is clicked.

EDIT: Here's a crude attempt at creating a manual parentItem: method, but it's not particularly intuitive. Surely there's a better way?

- (NSMenuItem *)findParentByChild:(NSMenuItem *)child {
    for(int x = 0; x < [statusBarMenu numberOfItems]; x++) {
        // Avoid any statically created menu items
        if([[statusBarMenu itemAtIndex:x] tag] != 100) {
            NSMenu *submenu = [[statusBarMenu itemAtIndex:x] submenu];
            if(submenu != nil) {
                for(int y = 0; y < [submenu numberOfItems]; y++) {
                    // This looks like our parent
                    if([submenu itemAtIndex:y] == child) {
                        return [statusBarMenu itemAtIndex:x];
                    }
                }
            }
        }
    }
    return nil;
}

What's the best way to go about achieving this in a way that's 10.5 and 10.6 compatible?

+1  A: 
Joshua Nozzi
D'oh! That teaches me for not reading closely enough. I've updated my question accordingly. Thanks!
ndg
Hmmm ... your question is no longer clear to me. I suggest modifying your question to include your actual overarching goal. Currently it reads like "how to implement this solution to an unnamed problem?" :-) Start with what you're trying to accomplish by getting the menu item's parent. To me it almost seems like a menu-item-to-model-object problem you might solve with judicious use of NSMenuItem's -representedObject, but I'm guessing wildly here based on your question.
Joshua Nozzi