Why is the NSStatusItem displaying multiple times?

202 views Asked by At

A NSStatusItem has a NSMenu attached, and one of the buttons of the NSMenu opens a NSWindow. Whenever one of these buttons is clicked, the window opens as expected and works properly, but another display of the NSStatusItem is opened.

The NSStatusItem is a clock, so I can see that it is updating correctly. However, the cloned NSStatusItem doesn't have its own menu. If I push the button that makes the window more times, more cloned versions of the NSStatusItem pop up.

Everything works fine except for this.

That's not a whole lot of information to go off of, but there's nothing else I can think of that could potentially help you. I would be happy to provide more information or try something.

EDIT: Every time the button is clicked, awakeFromNib is somehow called, which is why another half-working NSStatusItem happens.

EDIT: Temporary workaround is to put the awakeFromNib method in a dispatch_once.

EDIT: Added method that is triggered when button is clicked, as suggested by @zpasternack

- (IBAction)preferences:(id)sender {
    self.windowController = [[NSWindowController alloc] initWithWindowNibName:@"PreferencesWindow"];
    [[self windowController] showWindow:self];
}
1

There are 1 answers

3
zpasternack On BEST ANSWER

Is the NSStatusItem contained in the PreferencesWindow nib? That might explain it, since you're loading the nib each time the button is clicked.

Also, is there a reason you need to recreate that window each time the button is clicked? Maybe you could only do it the first time?

- (IBAction)preferences:(id)sender {
    if( self.windowController == nil ) {
        self.windowController = [[NSWindowController alloc] initWithWindowNibName:@"PreferencesWindow"];
    }
    [[self windowController] showWindow:self];
}