Question

I have an application that sits in the status bar on the Mac. It has a menu that lets you look at the app version, change some settings and quit the app.

I built it on Leopard and it works fine on Leopard. On Snow Leopard, I can see the app in the status bar and clicking on it shows the menu items. But clicking items in the menu doesn't do anything. The app isn't hung - it's doing stuff that it's expected to do. The UI thread isn't hung either - I can see the menu obviously, and and dialogs that the app causes to show on the UI thread (outside of the ones when the menu is clicked) show up fine. Compiling it on Snow Leopard targeted for 10.6 doesn't seem to change this behavior.

So I know that the app is able to create dialogs, isn't hung and is functioning. I suspect this has something to do with the menu, and handling menu clicks either in my app or in Snow Leopard.

Anyone else have suggestions on what this might be or where I should look? Any suggestions on where I can place a BP to trap whether the menu item click is actually happening?

EDIT: I've uploaded a simple repro 10.6 XCode project. I'll send a virtual beer your way if you can take a look and let me know if you see what's wrong. The app is simple - create a status bar app with 1 menu item "Quit" which calls terminate on the app. The status bar app show, the menu shows but clicking does nothing. Here's the project. It runs fine on Leopard (Clean All before you build/run) but not on Snow Leopard.

Was it helpful?

Solution

It sounds likely that your menu item's action outlet isn't "hooked up" to anything in your code. I don't know from your description whether or not you created the menu in interface builder or are creating it programmatically, but the first thing to do would be to check there. Make sure that if you're creating the menu programmatically, that you've spelled the method name correctly in the @selector() (if you've got warnings that say "[class] may not respond to -Foo" that's a big tip off of a typo).

If the connections are looking good, I'd break point the method set as your action and verify that you're actually calling the method you think you are.

If you're setting the connections programmatically, breakpoint the addItemWithTitle: (or equivalent call) call and ensure that your NSMenuItem setup correctly immediately after stepping over that line.

---EDIT---

After looking over the posted code, there are several fundamental flaws with it, any of which could be causing the breakage under 10.6.

  1. First, you're confusing the role of a delegate in relation to the menu. The delegate should not be your NSMenu object, your delegate should be an object that is invoked as a "call back" by the NSMenuItem to provide custom logic. You need not be extending NSMenu at all, and your MenuTestAppDelegate should only be extended from NSObject.

  2. The class of your Menu object in your MainMenu.xib file is of type MenuTest, but you don't have a MenuTest class declared in your project. Hopefully this is merely a copy/paste error while you were setting up your test project, but you should check it out.

  3. I find it far easier to set up the NSStatusItem's menu programmatically. Try this:


// create the menu
- (void)createMenu
{   
    NSMenu* aMenu = [NSMenu new];
    [aMenu setDelegate:self];

    NSMenuItem* quitItem = [[NSMenuItem alloc] initWithTitle:@"quitItem"
                           action:@selector(quitSelected:) 
                           keyEquivalent:@""];

    [aMenu addItem:quitItem];
    [quitItem release];

    NSStatusItem* statusItem = [[NSStatusBar systemStatusBar]
                               statusItemWithLength:NSVariableStatusItemLength];

    NSImage* itemImage = [[NSImage alloc] initWithContentsOfFile:
                         [[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"]];
    NSImage* itemAlternate = [[NSImage alloc] initWithContentsOfFile:
                             [[NSBundle mainBundle] pathForResource:@"alternate" ofType:@"png"]];

    [statusItem setImage:itemImage];
    [statusItem setAlternateImage:itemAlternate];

    [statusItem setHighlightMode:YES];
    [statusItem setMenu:aMenu];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top