Swift iOS Development: How to Pass Data from One Screen to Another

Jeff Garibay
Mac O’Clock
Published in
4 min readApr 6, 2020

When you begin working with Swift and developing apps for all sorts of businesses or great ideas, you will eventually need to pass data from one view controller to another or as many say, “from one screen to another”.

In this article, you will learn how to do this with a simple example. The process can seem quite challenging but refill your coffee cup, take a deep breath and know you will pull through this. It’s not that bad!

The Project

Let’s pretend your app is called Cool Cars. Cool Cars requires you to pass data from a starting view controller that displays a list of vehicles to the destination view controller that shows the selected vehicle details.

Setting Up the Segue

You will first control + drag from the starting view controller to the destination view controller you want to pass data to. Select what kind of segue you want, for this case we are using show.

Next, you will want to give the segue an identifier. You do this by clicking on the arrow that is now going to the destination view controller.

In this example, we went with carVCSegue to keep things descriptive. Meaning: this is the segue to the car view controller.

In order to present the destination view controller, you will want to use the performSegue method. You can add this method to an IBAction. For this tutorial, let’s use a button action.

As you can see we are utilizing the identifier we created to present the destination view controller when the button is pressed.

Now that you have the segue linked from one view controller to the other, it’s time to prepare the data to be passed!

Prepare to Pass the Data

In the starting view controller, you will want to use the prepare for segue method.

Inside this method, you are able to prepare the data that is to be used in the view controller that will be displayed. For the sake of keeping this article short, let's say we have already created a variable inside of the starting view controller. This variable is called car. Let’s also say we have created another variable inside of the destination view controller. This variable is called selectedCar.

Now, this is where the magic happens. Inside of your prepare for segue method, you will write the code required to pass data.

When the segue takes place, we are assigning our destination view controller to the carVC variable. Then, we are saying that the variable found in the destination view controller is to contain the data from the starting view controller.

carVC.selectedCar = car

Now, you can utilize the data in the destination view controller because you have passed the data right over. The data will be available to you through the variable selectedCar.

Now, we can get all sorts of information from selectedCar.

Conclusion

In the example that was used above, you would typically have a model that you have created which will hold the car data. For the sake of simplicity, the model information was not included in the example. However, the point being made is to utilize prepare for segue to prepare the data and pass the data to your destination view controller.

--

--