Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a window on click on NSStatusItem?

I'm pretty new to cocoa, so please excuse me for any stupid mistakes I make. I have a NSStatusItem, which I want to use to open up a menu. However as far as I know and have heard across different forms, without a custom view you are restricted to just a pop down menu. Is this true? And if so how do you make a custom view to do something (e.g. open a window in my case)? Thanks for any help.

like image 605
Luke Avatar asked Mar 26 '11 16:03

Luke


1 Answers

No, it is not true. You need to set up the target and action for the status item to call a method which does what you want (opens the window).

// This goes where you set up the status item
NSStatusItem *statusItem; // You need to get this from the status bar
[statusItem setTarget:self];
[statusItem setAction:@selector(openWindow:)];

// This method is called when the status item is clicked
- (void)openWindow:(id)sender {
    NSWindow *window = [self window]; // Get the window to open
    [window makeKeyAndOrderFront:nil];
}

You may also want to call [NSApp activateIgnoringOtherApps:nil]; to your openWindow: method to ensure that the window you open is not behind some other application's window.

like image 194
ughoavgfhw Avatar answered Nov 01 '22 18:11

ughoavgfhw