title: How do I instantiate a UIView / UITableView from a xib (nib) in a ViewController
body: <p>I want to display a user interface that has a segmented control, and a different table view for each section of the segmented control; so, 2 table views (buddies and bunches) that can be switched between.
  To implement this, I have done the following</p>
  
  <ol>
  <li>Create a ViewController in Storyboard</li>
  <li>Delete the View from the ViewController</li>
  <li>Create a new UIViewController swift class with an associated xib file</li>
  <li>Put the segmented control in the the main UIView in the xib</li>
  <li>Put a inner UIView element inside of the main UIView to take the space where the table views where replace it</li>
  <li>Created two subclasses of UITableView and corresponding xib files</li>
  </ol>
  
  <p>Some options I have thought of:</p>
  
  <ul>
  <li>I can set the class of the inner UIView in Interface Builder to be that of one of the table views, but I wouldn't know how to instantiate the other one in place of the initial one. If I created overlapping inner UIViews that each was associated with a table view and hiding one of them when I switch the segmented control, that actually kind of works, but the overlapping nature of views makes layout difficult and unintuitive.</li>
  <li><strong>What I want to know how to do</strong>: Instantiate the table views in place of the single main UIView element</li>
  <li>Alternative: Have one UITableView subclass that has a condition based on the state of the segmented control for what data it displays.  I don't like this as much because it will mix the code together for the table views.  In this case, I wouldn't even need to use xibs anymore, I could do this in the storyboard with just one table view.</li>
  </ul>
  
  <p>** ViewController Code **</p>
  
  <pre><code>@objc(BuddiesBunchesViewController) class BuddiesBunchesViewController: UIViewController {
  
      @IBOutlet weak var segmentedControl: UISegmentedControl!
  
      @IBOutlet weak var tableView: UIView!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // Do any additional setup after loading the view.
  
          // Instantiate tableView here to BuddiesTableView
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
      }
  
      @IBAction func segmentedControlIndexChanged(sender: AnyObject) {
          switch segmentedControl.selectedSegmentIndex {
          case 0: // Buddies
              // Set tableview to the buddies table view
          case 1: // Bunches
              // Set tableview to the buddies table view
          default:
              break;
          }
      }
  }
  </code></pre>
  
  <p>** Table View **</p>
  
  <pre><code>@IBDesignable class BuddiesTableView: UITableView, UITableViewDataSource, UITableViewDelegate {
  
      var view: UIView!
  
      var nibName: String = "BuddiesTableView"
  
      //init
  
      override init(frame: CGRect) {
          // set properties
  
          super.init(frame: frame)
  
          // Set anything that uses the view or visible bounds
          setup()
      }
  
      required init(coder aDecoder: NSCoder) {
          //set properties
  
          super.init(coder: aDecoder)
  
          // Setup
          setup()
      }
  
      func setup() {
          view = loadViewFromNib()
  
          view.frame = self.bounds
          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
      }
  
  // MARK: - Table View
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
           return 10
      }
  
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
          let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
          cell.textLabel?.text = "\(indexPath.row)"
          return cell
      }
  }
  </code></pre>