Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSWindowController showWindow: flashes the window

So I'm trying to open a new NSWindow like so:

NSWindowController *winCon = [[NSWindowController alloc] initWithWindowNibName:@"NewWindow"];
[winCon showWindow:self];

When I do this, the new window flashes on the screen, as in it appears and then quickly disappears. I know that I have my window correctly referenced in IB and everything. It's like it wants to show the window, but then it gets deallocated or something weird almost immediately. Any help will be appreciated.

like image 252
James Glenn Avatar asked Nov 29 '12 03:11

James Glenn


1 Answers

First, the name of the initializer isn't -initWithNibName:, but -initWithWindowNibName:.

Second, and this is true if you're using ARC, your window goes foom because you don't have a strong reference for your instance of NSWindowController. When the method ends, so does your reference.

If, say, you were to do this instead in your application delegate interface:

@property(strong) NSWindowController *winCon;

And synthesized it in your implementation file:

@synthesize winCon;

Then you could set up like this:

self.winCon = [[NSWindowController alloc] initWithWindowNibName:@"NewWindow"];
[self.winCon showWindow:self];

Now your window won't disappear. The window controller will be released when the application closes.

like image 118
Extra Savoir-Faire Avatar answered Nov 10 '22 13:11

Extra Savoir-Faire