0

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?

ndg
  • 2,585
  • 2
  • 33
  • 58

1 Answers1

2

You're saved: -menuNeedsUpdate: has been available since 10.3. :-)

Available in Mac OS X v10.3 and later. Available as part of an informal protocol prior to Mac OS X v10.6.

I use it heavily in an application that targets 10.5/10.6.

Note: This answer pertains to an earlier version of the question concerning availability of -menuNeedsUpdate:

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • D'oh! That teaches me for not reading closely enough. I've updated my question accordingly. Thanks! – ndg Aug 03 '10 at 15:07
  • 1
    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 Aug 03 '10 at 15:29