Passing data across views

Many applications required you to pass data values from one view to another. In iOS development passing data can be implemented using various techniques. Below I demonstrate a way to pass data using the storyboard in xCode 6.2 and I will be using Apple's Swift language.

Starting Off

Begin by opening xCode and click on create a new xCode project and for the purpose of staying in sync you can choose single page application like the image below.

Click Next, add a product name and make sure language is swift. Then click Next and Create.

Getting Set Up

Right Click on name of the project and add New File and name it whatever you want but in this example I named mine SecondViewController.swift

Next you are going to want to navigate to the ObjectLibrary(Red Box) as shown in screen shot and find the view controller tab then drag and drop it on Main.storyboard.

Then make sure SecondViewController.swift is connected to the new view controller object that we placed on Main.storyboard.

As shown above make sure your second view controller object is highlighted (Box). Go to Identity Inspector in the top right and under custom class use the Class drop down box(Arrow Right) to find the second view controller you have created and select it. So that now under custom class your class should be the same as the one in the projects folder(Arrow Left).

With all of that out of the way we can now goto Object Library(Box) and Drag a few different controls onto each of the views and connect them to appropriate view controller. In the case above the view on the left is connected to ViewController.swift and the one on the right is now connected to SecondViewController.swift. Here are the Controls that I added for each view and view controller.

View Controller

-UILabel
-UITextField
-UIButton

Second View Controller

-UILabel

Creating the Segue

We can now create a segue that will take us from the first view controller to the second view controller. To do so we press control, click and hold the Button in the first view and drag the line across to the second view as seen below.

After releasing the button a popover will appear and ask how we want to segue and for the purposes of this example we can choose show like seen below.

If we run the project now and click the button in the first view it will now segue us to the second view which contains the UILabel displaying the text "Label". In order to pass the data that we place in the UITextField in ViewController to the UILabel in the SecondViewController to be displayed then we must first add a little bit of code.

Coding The Controllers

So make sure that you have set up the proper outlet to the UITextField Control in ViewController.swift.

@IBOutlet weak var textField: UITextField!

In the SecondViewController.swift we should have two variables secondVCData and label. The secondVCData will catch the data being passed and the label will display the data as a UILabel.

    var secondVCData: String!
    @IBOutlet weak var label: UILabel!

Now we a string in the Second View Controller to catch the data from the first View Controller and the next thing todo is to implement an overridden function into ViewController.swift that will look like this

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let secondVC = segue.destinationViewController as SecondViewController
        secondVC.secondVCData = textField.text
     
    }

In the code above we make a variable secondVC as our segue destination view controller and we initialize it as our second view controller. Once we have the controller initialized we are then able to access the SecondViewControllers' variables(secondVCData) and data from the first ViewControllers' UITextField.

Both view controllers should contain similar or the same exact code as seen below.

When running the project, right under the label displaying "Pass to Second View Controller there is a UITextField waiting for input. Input text in the text field and click the button directly to the right and then your second view controller will pop up displaying a UILabel with text of whatever we input in the UITextField in the previous controller.