Search code examples
iosobjective-cuiviewcontrollersegueuistoryboardsegue

How to implement prepareForSegue when I am not working with interface builders?


I am moving from the world of storyboards to the one without. I was wondering how to implement the prepareForSegue method in this case. Because this method uses a segue identifier and the only way I know to provide a segue identifier is through the storyboard, I am not sure how to do this when I am not using any storyboard.


Solution

  • Vishisht,

    You are going to want to override the prepare(for:sender:) function in your ViewController.

    You are going to want to configure segue identifiers and then you can configure your segue as so:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "mySegueIdentifier" {
            if let destination = segue.destination as? SomeViewController {
                // Configure your destination VC
            }
        }
    }
    

    That being said, if you are not using storyboards you should probably move away from using segues. I personally find it easier to present view controllers when I instantiate them programmatically and have a coordinator manage them. If you are curious about this, I can provide some examples.