title: Swift – Instantiating a navigation controller without storyboards in App Delegate
body: <p>I'm rebuilding an app without storyboards and the part of it that I'm having the most trouble with is navigating view-to-view programatically. Few things are written out there which don't use storyboards, so finding an answer for this has been tough.</p>
  
  <p>My problem is pretty simple. I have my <code>ViewController</code> and my <code>SecondViewController</code> and I want to push from the former to the latter.</p>
  
  <p>In <code>AppDelegate</code>:</p>
  
  <pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
      // Override point for customization after application launch.
  
      window = UIWindow(frame: UIScreen.mainScreen().bounds)
      window?.backgroundColor = UIColor.whiteColor()
      window?.rootViewController = ViewController()
      window?.makeKeyAndVisible()
  
      return true
  }
  </code></pre>
  
  <p>Then in <code>ViewController.swift</code>:</p>
  
  <pre><code>class ViewController: UIViewController, AVAudioPlayerDelegate, UITextFieldDelegate {
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          startFinishButton.setTitle("Begin", forState: .Normal)
          startFinishButton.addTarget(self, action: "moveToSecondViewController", forControlEvents: .TouchUpInside)
          view.addSubview &lt;*&gt; startFinishButton
      }
  
      func moveToSecondViewController(sender: UIButton) {
          let vc = SecondViewController()
          println(self.navigationController) // returns nil
          self.navigationController?.pushViewController(vc, animated: true)
      }
  }
  </code></pre>
  
  <p>Printing <code>self.navigationController</code> returns nil. I've tried doing:</p>
  
  <p><code>var navController = UINavigationController()</code> when the <code>ViewController</code> class is created (but outside of ViewDidLoad, right under the class declaration) and done the push using the <code>navController</code> var but that hasn't worked. </p>
  
  <p>I'm thinking maybe the solution is to create a navigation controller in App Delegate that the whole app would use, I guess as a global variable?</p>
  
  <p>My hope is that this post can serve many others who are new to Swift and want to remove storyboards from their app.</p>
  
  <p>Thanks for taking a look and for your help.</p>