How do I start an OS X user interface with a button with focus?
<p>I have an intermittent problem that is driving me crazy. I have an OS X app in Xcode that has a simple user interface with a button and a few text fields.</p>

<p>Sometimes it starts <strong>with the focus on the button</strong>, which is the behavior I'm seeking, as I have the button mapped to the carriage return key in IB. </p>

<p>But sometimes it starts <strong>without the focus on the button</strong>, and then the carriage return key doesn't work as a button press.</p>

<p>I've tried to set the button as first responder in <code>init()</code> with <code>self.buttonName.becomeFirstResponder()</code> but that doesn't seem to make much difference as to when the app comes up with the button highlighted and ready to accept a carriage return or not.</p>

<p>I feel like an idiot, as I've been working on this seemingly simple problem for over a week now. Does anybody have any ideas?</p>

<p>@Meriw suggests that I put the first responder call into viewWillAppear. I have in AppDelegate.swift:</p>

<pre><code>@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var mainWindowController: MainWindowController?

    func applicationDidFinishLaunching( aNotification: NSNotification ) {

        // Create a window controller
        let mainWindowController = MainWindowController()

        // Put the window of the window controller on screen
        mainWindowController.showWindow( self )

        // Set the property to point to the window controller
        self.mainWindowController = mainWindowController
    }

    func applicationWillTerminate( aNotification: NSNotification ) {
        // Insert code here to tear down your application
    }

    // Make the program disappear after the window is closed
    func applicationShouldTerminateAfterLastWindowClosed( sender: NSApplication ) -&gt; Bool {
        return true
    }

}
</code></pre>

<p>And in MainWindowController.swift:</p>

<pre><code>import Cocoa

class MainWindowController: NSWindowController {

    init() {
        super.init( window: nil )

        // load the contents of the nib, and set the owner as self, which connects the oultlets
        NSBundle.mainBundle().loadNibNamed( windowNibName, owner: self, topLevelObjects: nil )
    }

    required init?( coder: NSCoder ) {
        super.init( coder: coder )
    }

    override var windowNibName: String {
        return "MainWindowController"
    }
</code></pre>

<p>...</p>

<p>Where would viewWillAppear be implemented and how?</p>