Luke
Luke

Reputation: 105

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.

Upvotes: 9

Views: 5672

Answers (1)

ughoavgfhw
ughoavgfhw

Reputation: 39935

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.

Upvotes: 17

Related Questions