Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSWindowController showWindow:nil does nothing

Like in the title, [myWindowController showWindow:nil] doesn't work. Here are some facts you may need to know:

  • My window controller: KRAuthenticationWindowController
  • Interface builder file: AuthenticationWindow.xib
  • File's Owner is KRAuthenticationWindowController
  • window outlet is connected to the window
  • Window's delegate is connected to File's Owner
  • Window's Visible at launch is unchecked
  • Window's Release when closed is also unchecked

My code is presented below:

// KRApplicationDelegate.m

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    NSLog(@"%s",__PRETTY_FUNCTION__);
    KRAuthenticationWindowController *authWindowController = [[KRAuthenticationWindowController alloc] init];
    [authWindowController showWindow:nil];
    [[authWindowController window] makeKeyAndOrderFront:nil];
}

// KRAuthenticationWindowController.m

- (id)init {
    self = [super initWithWindowNibName:@"AuthenticationWindow"];
    if(!self) return nil;
    NSLog(@"%s",__PRETTY_FUNCTION__);
    return self;
}

- (void)loadWindow {
    [super loadWindow];
    [self.window setBackgroundColor:[NSColor colorWithDeviceWhite:0.73 alpha:1]];
    NSLog(@"%s",__PRETTY_FUNCTION__);
}

- (void)windowDidLoad {
    [super windowDidLoad];
    NSLog(@"%s",__PRETTY_FUNCTION__);
}

- (void)showWindow:(id)sender {
    [super showWindow:sender];
    NSLog(@"%@",self.window);
    NSLog(@"%s",__PRETTY_FUNCTION__);
}

My console output:

2013-02-24 16:21:45.420 Application[3105:303] -[KRApplicationDelegate applicationDidFinishLaunching:]
2013-02-24 16:21:45.421 Application[3105:303] -[KRAuthenticationWindowController init]
2013-02-24 16:21:45.428 Application[3105:303] -[KRAuthenticationWindowController loadWindow]
2013-02-24 16:21:45.428 Application[3105:303] -[KRAuthenticationWindowController windowDidLoad]
2013-02-24 16:21:45.556 Application[3105:303] <NSWindow: 0x10016e860>
2013-02-24 16:21:45.556 Application[3105:303] -[KRAuthenticationWindowController showWindow:]

I think I'm just missing something important. Any help would be appreciated.

like image 595
akashivskyy Avatar asked Feb 24 '13 15:02

akashivskyy


1 Answers

Try turning authWindowController into an instance variable. Currently, it's a local variable. When the local variable goes away, the window controller may get released and the window with it, so it never gets shown.

like image 161
user2015453 Avatar answered Nov 16 '22 02:11

user2015453