How to open a window when clicking on NSStatusItem?

I am new to cocoa, so please excuse me for any stupid mistakes I make. I have an NSStatusItem that I want to use to open a menu. However, as far as I know and have heard in different forms, without a special presentation, you are limited only to the pop-up menu. It's true? And if so, how do you create a custom view to do something (for example, open a window in my case)? Thanks for any help.

+7
source share
1 answer

No that's not true. You need to set a goal and action for the status element in order to call a method that does what you want (opens a 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 can also call [NSApp activateIgnoringOtherApps:nil]; to your openWindow method: to make sure that the window that opens is not behind another application window.

+14
source

All Articles