views:

204

answers:

2

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?

+2  A: 

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

Nikolai Ruhe
Thanks but it still doesn't work. I added the [window makeKeyAndOrderFront:self]; call straight after the showWindow: and it still doesn't work. Should I be calling this somewhere else?
Luke
`makeKeyAndOrderFront:` is the right method. It shouldn't matter where you call it. Something else is the problem. For instance, the main window might be modal, or something is refusing to give up first responder status.
Tom Dalling
Yes, there was a problem within the nib, see my answer for the solution.
Luke
Thanks you for your help!
Luke
This worked fine along with showWindow: once the nib was setup correctly.
Luke
+1  A: 

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

Luke