title: Programmatically segue from MainViewController to .xib view in Swift?
body: <p>I'm having trouble finding help on performing a segue, or some kind of view transition with swift with at least iOS 8 or later. I'm trying to avoid using the Storyboard editor as much as possible, aside from implementing a segue from the Main View Controller to the xib.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>    let bundle = NSBundle(forClass: self.dynamicType)
      let secondViewController = ViewController(nibName: "SwitchRegionSegue", bundle: nil)
      self.presentViewController(secondViewController, animated: true, completion: nil)
  </code></pre>
  
  <p>The code above is called from the main ViewController.swift using the Single View Application new project scaffold. But it results in a crash: </p>
  
  <p><code>*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "SwitchRegionSegue" nib but the view outlet was not set.'</code></p>
  
  <p>I also have these two files like so:</p>
  
  <p><code>SwitchRegionSegue.xib</code></p>
  
  <p><code>SwitchRegionSegue.swift</code></p>
  
  <p>I've made sure that the file's owner custom class for the xib is set to <code>SwitchRegionSegue : UIView</code> </p>
  
  <p>My SwitchRegionSegue.swift is just a blank canvas and looks like this:</p>
  
  <pre><code>import UIKit
  
  class SwitchRegionSegue: UIView {
  
      var view: UIView!
  
      var nibName = "SwitchRegionSegue"
  
      func xibSetup() {
          view = loadViewFromNib()
  
          // use bounds not frame or it'll be offset
          view.frame = bounds
  
          // Make the view stretch with containing view
          view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
          addSubview(view)
      }
  
      func loadViewFromNib() -&gt; UIView {
  
          let bundle = NSBundle(forClass: self.dynamicType)
          let nib = UINib(nibName: nibName, bundle: bundle)
          let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
  
          return view
      }
  
      override init(frame: CGRect) {
          super.init(frame: frame)
          xibSetup()
      }
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          xibSetup()
      }
  
  }
  </code></pre>
  
  <p>Anyone know what it is that I'm not doing correctly here? (suggestion to a good swift &amp; xib textbook would be awesome)</p>