1

I've got an application which toggle's a NSStatusItem from the preferences window. The preferences window has a checkbox "enabled/disabled" which calls the "enabledStatusItem" and "disableStatusItem"-methods in the class NSStatusItem.

This all works fine, my problem is adding a menu to this NSStatusItem. The code in the class NSStatusItem looks like this:

-(void)enableStatusItem
{
    //get icon
    theIcon = [NSImage imageNamed:@"test.png"];

    //create item
    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
    [statusItem setImage:theIcon];
    [statusItem setHighlightMode:YES];
    [statusItem setMenu:statusMenu];
}

-(void)disableStatusItem
{
    [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
}

Everything works, except setMenu. At first I loaded the StatusItem in awakeFromNib, then setMenu worked. But the StatusItem has to stay off when the preference checkbox is "off", so I couldn't load it in awakeFromNib anymore.

I suspect setMenu doesn't load because it isn't in awakeFromNib, but "enableStatusItem" is called after awakeFromNib, so in awakeFromNib there isn't a statusItem yet to add the menu to. And I can't "preload" the statusItem because it is created with "[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];"

I've tried a lot so far, even made a test-application where I added every piece of code one by one, so I could break it down. But without any success so far.

I feel because it's such a simple thing, only one little NSMenu, isn't there a simple solution then?

Maybe one of you guys has an idea?

Thanks in advance for your time, Greetings Frans

Frans
  • 57
  • 7

1 Answers1

1

that code is ok, you have the issue that the menu is not retained

assert(statusMenu);
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Hi Daij-Djan, I've put assert(statusMenu); after the setMenu-line, but it doesn't work, it gives an error. I use ARC, so maybe that's the problem? – Frans Dec 16 '12 at 17:50
  • Can u also show the header file with the declarations of statusItem and statusMenu? –  Dec 16 '12 at 18:07
  • { IBOutlet NSMenu* statusMenu; NSImage* theIcon; NSStatusItem* statusItem; } -(void)enableStatusItem; -(void)disableStatusItem; – Frans Dec 16 '12 at 18:53
  • Apparently I didn't google hard enough, I found my answer: http://stackoverflow.com/questions/8557129/creating-status-item-icon-shows-up-menu-doesnt. I created a new nib file for the menu, before that everything was in the mainmenu nib file which is owned by the application. I changed the file owner of the new nib to the statusitem-class. I call that nib file in init and now it works! Thanks for your help guys – Frans Dec 16 '12 at 20:30