Nifty Ways of Passing Data Between View Controllers (Part 1/2)

There are several ways of passing data between View Controllers. In part one of this series, we will focus on getting our First View Controller to pass its data to Second View Controller.

There are primarily three ways of accomplishing this:

  1. Storyboard Only
  2. Hybrid of Storyboard and Coding
  3. Programmatically Only

Storyboard Only Approach

Arguably the simplest way to pass data is through storyboards. To start, hold the control key, click on a UI Control (a UIButton in our case), and drag to Second View Controller. The pop-up menu will display different types of transitions. Select ‘Show’.

This sets up a segue from your UI control to the Second View Controller. Now any time a user interacts with our button, your app will automatically transition to displaying Second View Controller.

Don’t forget to give our segue an identifier so we can refer to it in code. We will call ours showSecondViewController. Typically your first word should be a verb that helps describe what the segue accomplishes.

Now we will jump into First View Controller and write the following code:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecondViewController" {
guard let secondViewController = segue.destination as? SecondViewController else { return }
// Pass Data to Second View Controller
secondViewController.dataStorageVariable = dataToPass

And that is the whole of it!

Hybrid Approach

While the previous approach works, sometimes you want control over when the segue begins transitioning. In these conditions, the hybrid approach is a life saver.

You start by setting up a segue by control clicking on the view controller icon instead of a UIControl. This is the yellow circle found at the top of First View Controller in storyboards.

Just as before, you also set the segue’s identifier so you can later pass data in prepare(for: segue)

All that remains is to call performSegue(withIdentifier: “showSecondViewController”, sender: self). This will begin the segue at a time of your choosing, and data will pass in prepare(for segue) just as before.

Programmatic Approach

It is important to understand just what storyboard segues are doing for us when we transition from one View Controller to the next. That way we will know what we must replicate in our code. It turns out it is not complicated:

  1. Instantiate the destination View Controller
  2. Present the new View Controller
// Instantiate Second View Controller
if let secondViewController = storyboard?.instantiateViewController(withIdentifier: "secondViewController") as? SecondViewController {
// Pass Data
secondViewController.dataStorageVariable = dataToPass
// Present Second View
present(secondViewController, animated: true, completion: nil)

One last thing: We need to add a Storyboard ID for our Second View Controller. This will be the Identifier used when calling storyboard.instantiateViewController(withIdentifier:)

Run the code and you should be good to go!


To summarize, the three ways to pass data are:

  1. Storyboard: Segue automatically handles View Controller transition.
    Override and pass data in prepare(for: segue) function.
  2. Hybrid: Segue transitions only when you call performSegue(withIdentifier: sender:).
    Data passed in prepare(for: segue).
  3. Programmatically: No segue created in storyboard.
    Call storyboard?.instantiateViewController(withIdentifier:) to create an instance of Second View Controller.
    Pass data manually and call present(viewControllerToPresent: animated: completion:) when you are ready to transition to Second View Controller.

Congratulations! As programmers it is always good to know multiple ways of accomplishing a task, and using the best one for any given scenario. In Part 2 we will focus on passing data back to the First View Controller. Keep your eyes peeled!