8

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.

1 Answer 1

16

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.