0

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];
}
Eliza Wilson
  • 1,031
  • 1
  • 13
  • 38
  • Can you post the code that runs when the button is clicked that opens the NSWindow? – zpasternack Dec 12 '13 at 22:08
  • There you go @zpasternack. Also, `windowController` is declared in the header like this `@property (strong) NSWindowController *windowController;` – Eliza Wilson Dec 12 '13 at 22:16

1 Answers1

0

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];
}
zpasternack
  • 17,838
  • 2
  • 63
  • 81
  • No, the NSStatusItem is not in PreferencesWindow. I have PreferencesWindow which is opened by preferences, and then MainMenu.xib which is where the NSStatusItem is. I'm going to read up on how `awakeFromNib` actually works, since it could be called if any nib is opened that the object owns, not just MainMenu – Eliza Wilson Dec 12 '13 at 23:20
  • "It is recommended that you maintain a one-to-one correspondence between your File’s Owner objects and their associated nib files. Loading two nib files with the same File’s Owner object causes that object’s awakeFromNib method being called twice, which could cause some data structures to be reinitialized in undesired ways." https://developer.apple.com/library/mac/documentation/cocoa/reference/applicationkit/Protocols/NSNibAwaking_Protocol/Reference/Reference.html – Eliza Wilson Dec 12 '13 at 23:23
  • I need to separate any additional xibs into a separate controller – Eliza Wilson Dec 12 '13 at 23:29