title: How to give focus to NSWindow loaded from NIB?
body: <p>I'm using <code>NSWindowController</code> to load a window from a NIB. However, when I call <code>showWindow:</code>, the window is visually topmost, but the focus remains where it was (instead of moving it to the new window).</p>
  
  <p>It's easy to see this happening when the first window (with keyboard focus) is moved slightly, before creating the new window (via cmd+n). This is the result:<br>
  <img src="https://i.stack.imgur.com/DumNO.png" alt=""><br>
  The bottom, focused window is the original window. The unfocused window on top is the newly created window.</p>
  
  <p>This is the relevant code:</p>
  
  <p>AppDelegate.h:</p>
  
  <pre><code>- (IBAction)newDocument:(id) sender;
  </code></pre>
  
  <p>AppDelegate.m:</p>
  
  <pre><code>- (IBAction)newDocument:(id) sender {
      [[[FooController alloc] init] showWindow:self];
  }
  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
      [self newDocument:self];
  }
  </code></pre>
  
  <p>FooController.h:</p>
  
  <pre><code>@interface FooController : NSWindowController { }
  @end
  </code></pre>
  
  <p>FooController.m:</p>
  
  <pre><code>- (id)init {
      self = [super initWithWindowNibName:@"FooWindow"];
      return self;
  }
  </code></pre>
  
  <p>FooWindow.xib:<br>
  A freshly created Window xib, without modifications.</p>
  
  <p>MainMenu.xib:<br>
  The default MainMenu.xib, with its window deleted.</p>
  
  <p>Calling <code>makeKeyAndOrderFront:</code> on the window in the controller's <code>windowDidLoad</code> method does not appear to focus the new window. Setting the File's owner of FooWindow.xib to <code>FooController</code> also did not appear to help.</p>
  
  <p>What is the correct way to load and show a window from a NIB so that it does receive keyboard focus?</p>
  
  <p><strong>Edit:</strong> It looks like <code>NSWindowController</code>'s <code>window</code> method returns <code>nil</code>, which explains why calling methods on <code>window</code> doesn't do anything. But why is it <code>nil</code>?</p>