Kevin
Kevin

Reputation: 1189

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.

Upvotes: 2

Views: 81

Answers (3)

Daniel Zhang
Daniel Zhang

Reputation: 5858

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.

Upvotes: 1

Lumialxk
Lumialxk

Reputation: 6369

Segue is between view controllers. So you can't do that in xib as it is only views. You should present/push view controllers in table view's view controller. You can get its view controller like this:

tableView.viewController // and then present/push

Upvotes: 1

Yann Bodson
Yann Bodson

Reputation: 1654

If your cell and your view controller are in different storyboards, you can (since iOS 9) use a segue between them with storyboard references.

See Adding a Segue Between Two Storyboards.

If your cell is in its own .xib unfortunately it is not possible to use a segue.

Upvotes: 1

Related Questions