Josh
Josh

Reputation: 81

Implementing NSPopover on NSStatusItem

I currently have an NSStatusItem for the status bar that is initialized in awakeFromNib like this:

    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
    [statusItem setMenu:statusMenu];
    [statusItem setImage:statusImage];
    [statusItem setAlternateImage:statusHighlightedImage];
    [statusItem setHighlightMode:YES];
    [statusItem setTarget:self];

I have an NSMenu that opens when the icon is clicked. One of the options, when clicked, is supposed to open an NSPopover from the status bar icon. To do that, I have this code, which is connected to the menu item via Interface Builder:

- (IBAction)prefs:(id)sender {    
    NSRectEdge prefEdge = NSMaxYEdge;

    [[self pop] showRelativeToRect:[[statusItem view] bounds]
                          ofView:[statusItem view]
                   preferredEdge:prefEdge];
}

However, when I click the menu item that should open the Popover, nothing happens. Is there any particular reason why this might be the case? As far as I can tell, it should be possible to do.

Thanks in advance. I looked through Stack Overflow the best I could, but if there's a true duplicate question, please link me to it.

FYI: The statusItem variable is the NSStatusItem.

Upvotes: 4

Views: 1814

Answers (2)

Motti Shneor
Motti Shneor

Reputation: 2194

I don't know if this problem was resolved already, but I have few notes.

  1. First step is to verify (emit NSLog, or stop at breakpoint in debugger) that your menu-item actually works, and that prefs: IBAction is being called.
  2. I don't know your UI design, but it makes sense to show the popover relative to the NSMenuItem which commanded it to appear, and not relative to the NSStatusItem. So use the [(NSMenuItem *)sender view] as your anchor and its bounds.
  3. try to display your popover from somewhere else in your app - just to make sure the actual popover is OK and can be displayed.

Upvotes: 0

Wevah
Wevah

Reputation: 28242

You need to set a view yourself using [statusItem setView:] for [statusItem view] to return non-nil.

If you don't mind using private API, you could try calling [statusItem _button], instead, though this won't be allowed if you plan on submitting to the App Store.

Upvotes: 2

Related Questions