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

4

1 に答える 1

1

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

assert(statusMenu);
于 2012-12-16T16:51:42.110 に答える