Jeff Swensen
Jeff Swensen

Reputation: 3573

Create NSMenu for NSStatusBar.systemStatusBar programmatically

I'm trying to create a simple menu in the System Status Bar using code only. I'm not receiving any compilation or runtime errors but I see no effect at all.

- (void)awakeFromNib
{
    NSMenu *stackMenu = [[NSMenu alloc] initWithTitle:@"Status Menu"];
    NSMenuItem *soMenuItem = 
        [[NSMenuItem alloc] initWithTitle:@"Status Menu Item" action:nil keyEquivalent:@"S"];
    [soMenuItem setEnabled:YES];
    [stackMenu addItem:soMenuItem];
    statusItem = [[[NSStatusBar systemStatusBar]
                   statusItemWithLength:NSVariableStatusItemLength]
                  retain];
    [statusItem setMenu:stackMenu];
}

Upvotes: 2

Views: 2616

Answers (1)

Sam
Sam

Reputation: 2707

I don't believe the NSStatusItem will implicitly take on the title of the NSMenu associated with it (which is what I am guessing you want to happen.) Try explicitly setting the NSStatusItem's title (and/or its image).

e.x.

[statusItem setTitle:[stackMenu title]];

Upvotes: 5

Related Questions