Kevin Kevin - 3 months ago 9
Swift Question

Swift Segue Issue

I am interested in segueing from an xib file (when a button is tapped) which is one table view cell from a table view, to another view controller. I am unable to show segue (through main storyboard) because they aren't on the same storyboard. I was wondering if this could be done programmatically, and if so, with what kind of code.

Please let me know if you need more detail.

Answer

Your options for programmatically showing a view controller are the following functions

From within a view controller

  • presentViewController
  • dismissViewController

From within a view controller on a navigation stack

  • pushViewController
  • popToViewController
  • popToRootViewController
  • popViewController

Which function to use is determined by the context in which the view controller appears.

For presenting view controllers, if you are working with XIBs and storyboards then the usual pattern is to first instantiate a view controller from the XIB or storyboard and then present it using presentViewController.

An example is

let vc = UIViewController(nibName: "NAME_OF_THE_NIB", bundle: NSBundle.mainBundle())
self.presentViewController(vc, animated: true) {
    // Add completion code here.
}

With navigation stacks you can use the convenience functions for pushing and popping view controllers.

To be clear about terminology, none of these methods are considered to be a Segue. That term only applies to moving between view controllers in a storyboard.

Comments