0
votes

I have a NSWindowController subclass, BBPreferencesWindowController

@implementation BBPreferencesWindowController

- (NSString *)windowNibName
{
    return @"PreferencesWindow";
}

...

And a function in my AppDelegate that can open the window in the "PreferencesWindow.xib" through this controller.

This function is called from an NSMenuItem, attached to an NSMenu under an NSStatusItem item in the system menu bar.

@property (strong) BBPreferencesWindowController *preferencesWindow;

...

- (void)openPreferences
{
    if (self.preferencesWindow == nil)
    {
        self.preferencesWindow = [[BBPreferencesWindowController alloc] init];
    }

    [self.preferencesWindow showWindow:self];
    NSLog(@"%@", self.preferencesWindow.window.isVisible ? @"YES" : @"NO");
}

The window is showed fine the first time I click the NSMenuItem (although the NSLog line logs "NO"), but when I close the window and then try to re-open it by click on the NSMenuItem for a second time, the window won't open.

What am I missing?

Thanks!

Edit: BBPreferencesWindowController doesn't have a custom init method. It does have a custom awakeFromNib (that gets called the first time)

- (void)awakeFromNib
{
    [super awakeFromNib];
    NSLog(@"Loaded!");
}
1
What do you do in the BBPreferencesWindowController's init method? Post it. The default initializer should be initWithWindowNibName:owner: . - Ramy Al Zuhouri
In your openPreferences, you only create a new controller if self.preferencesWindow is nil, which it won't be unless you clear it for some reason. Are you doing so when the window closes? - gaige
No. How would I implement this in the best way? A delegate in the AppDelegate or destroy the controller in windowDidClose? - brtdv
Couldn't there be a way to re-open the window without clearing the window's controller? - brtdv
It's going to depend on how your windows are set up in the XIB. If they're set to be destroyed on close, you won't get any response and will probably want to create a new controller. If they're just hidden on close, this should work. - gaige

1 Answers

3
votes

I found the reason why my BBPreferencesWindowController did not manage the window well: in the XIB, the File's Owner window outlet wasn't linked correctly.

Fixing this resolved all other problems as well.

Thanks for your help!