开发者

Create NSMenu for NSStatusBar.systemStatusBar programmatically

开发者 https://www.devze.com 2023-02-19 02:27 出处:网络
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.

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];
}


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]];
0

精彩评论

暂无评论...
验证码 换一张
取 消