Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open an NSWindow and have the window selected and in focus?

I am trying to open a NSWindow using the following code:

NSWindowController *window = [[NSWindowController alloc] initWithWindowNibName:@"MainWindow"];
[window showWindow:nil];

The window opens okay but the previous window is still the mainWindow and in focus. I have tried the following code to force the main window and it doesn't work. The window still has a disabled title bar and isn't accepting key events etc.

[self.window makeKeyAndOrderFront:self];
[self.window makeMainWindow];

The only way I seem to be able to get the previous window to lose focus is if I close the window after calling showWindow: with [[NSApp mainWindow] close];

Any ideas?

like image 477
Luke Avatar asked May 12 '10 10:05

Luke


2 Answers

makeKeyAndOrderFront: is the way to go. Are you sure that self.window and window refer the same object?

like image 170
Nikolai Ruhe Avatar answered Nov 14 '22 06:11

Nikolai Ruhe


I resolved the issue by assigning the WindowController to the nib File Owner, instead of having a separate NSWindowController object within the nib.

like image 24
Luke Avatar answered Nov 14 '22 07:11

Luke