How to Pass Data Between iOS Views in Swift

This tutorial will walk you through the process of passing data from one view to another in an iOS 8 application using Swift. Let’s get started!

There are a few steps to the process but once done you will see it is pretty simple to do provided you get everything in place. I am going to step over a few of the details that I think by now you should be familiar with like creating a new project, outlets and actions. If these are still a little new to you I strongly recommend looking through some of my other articles that cover those topics and more

First create a new Swift based iOS project with a single view to keep the code base to a minimum and allow us to focus on the details we are really interested in.


Rz Rasel

Now create a second view controller by searching and dragging one from the object library (lower right panel, third icon). We will pass two simple strings between the views, so for the first view controller add two text entry fields and a button. For the second add two labels which will display the text you enter on the first view.

02 - Two view controllers with controls.

To pass the data and change views we use a segue to move between these two views (I have added an extra step here incase I want to do anything extra when the button is clicked), right click the button and drag to the second view controller and release. From the popup menu select Action Segue > show, this sets up the segue to take place when the button is pressed. See the video below if you need extra help to make sense of it.

Click Here to View Video – Creating segue between view controllers.

Make sure you are using the first view controller and open the assistant editor (second icon on the right hand side of the xcode icon bar, it looks like a jacket and bow tie), you should see the view controller.swift file appear. Right click and drag from the first text entry field to the controller code and create an @IBOutlet named ‘fieldA’, then do the same with the second text entry field and name it ‘fieldB’. I also added an @IBAction for the button but it is not needed for the segue to take place, I just added it in case I want to do some extra things when it is clicked. The code for the view controller should now look something like this.

04 - First View Controller with Outlets and Action.

The icon that you see between the two views is called a segue, you will most likely end up using more than one in an application so for that reason I strongly recommend naming them all so they can be uniquely identified in code. So go ahead and click on the segue icon and in the right hand side attributes view give it the name ‘btnSubmitSegue’ as seen in the screenshot below.

05 - Segue Identifier

For the second view controller we are going to create our own UIViewController class as the one provided has a lot of code that you just do not need to be confused with at this time! So in the file menu create a new iOS Cocoa Touch Class.

06 - New iOS Cocoa Touch Class

I named my class ‘SecondViewController’, make sure you have the language set to Swift.

07 - SecondViewController Class Creation

Next we need to assign our new custom view controller class to our view, to do that select the second view controller and in the right hand identity inspector panel under ‘Custom Class’ in the field named ‘Class’ enter the name of the file you just created, in my case ‘SecondViewController’, you should notice as you start typing it that auto complete will try and help you out. A little side tip here is that Xcode is great at trying to help you complete many things, so if you find auto complete is not working then it might be a sign you got something wrong.

08 - Second View Controller with custom Class

The assistant editor for the second view controller should now be showing the nice clean version of our new class file. Right click on the labels in the view one by one and drag to the code window to create two new @IBOutlets called labelA and labelB. These will be used to reference the text labels on the view and populate them with the entries from the first screen.

09 - Second View Controller with Outlets

Now we need to add some properties to our second view controller class so that we can assign our data from the first view during the segue and hand it off to the new view. To do that create two variables, see the screenshot below. For simplicity I called my two variables (one per display label) ‘dataPassed’ and ‘secondDataPassed’.

10 - Second View Controller Class with Outlets and Variables

Now the next step (yes I know it seems like a lot of steps!) is to create code that assigns the entry field text to our variables in the second view, we do this by overriding something calledprepareForSegue. As the name suggests we are overriding the default with our code, copy and paste or enter the code below in ‘ViewController.swift’ and see the screenshot for reference.

11 - Segue Code

Finally! In our second view controller class file (secondViewController.swift) we need to assign the variables to the display labels when the viewDidLoad is called. This is straight forward, just look for the override func viewDidLoad() and enter the code below (again see screenshot for reference).

12 - Second View Controller with assignment code

We are done! You have reached the rainbow, run the simulator and enter some text into the two fields then hit the button, the second view should load showing the text you entered.

Click Here To View Video – Testing The Application

Hopefully this has cleared up some of the mystery for you and set you on a path to multi-view happiness!

I have included a link to a sample project

One comment on “How to Pass Data Between iOS Views in Swift

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s