iOS Tutorials: Passing Information to a Second View Controller

In this tutorial, we will learn how to pass information from the first view controller to the second view controller. In other words, whatever information that are being stored on the first view controller will then appear again on the second view controller.

On your Xcode, go to File > New > Project to create a new blank project.

Go ahead and give your project any name. For mine, I put Pass Information. You could go ahead and put in the required information as your own information.

Drag another View Controller into the Main.storyboard. For the first view controller, we will be needing TextField and Button while on the second view controller, we are gonna need a Label and a Button. By now you should have also created a new View Controller class which in my case was given the name SecondViewController.

Make sure that there you have two ViewController on your Main.storyboard and two .swift file. On the Main.storyboard, click on the Second View Controller and some of the settings will popped up on the right. We want to make sure that the Second View Controller is connected to SecondViewController.swift or else we won’t be able to connect any of those outlets to the swift file.

Connect those label and button to your ViewController except for the button on first view controller. Lets proceed to the coding section for the first ViewController

import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var textField: UITextField!
    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destination = segue.destination as? SecondViewController
            destination?.valuePassed = textField.text!

The SecondViewController.swift code should look like the following:

import UIKit
class SecondViewController: UIViewController {
var valuePassed: String = ""
@IBOutlet weak var labelValue: UILabel!
override func viewDidLoad() {
    labelValue.text = valuePassed
@IBAction func exitButton(_ sender: Any) {
        dismiss(animated: true, completion: nil)

Hopefully you’ll be able to understand from the code here. Happy Coding!

The following code is posted on GitHub

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

  • What’s new in Swift 4

    Apple Worldwide Developer Conference (WWDC) is a conference held annually by Apple to showcase new software and technologies for developers. […]

  • iOS Tutorials: UICollectionView

    In this tutorial, we’ll be focusing on CollectionView and the easiest way to go around this is to first create […]

  • What is array?

    Array is an Ordered Collection Type and array is used to organize data where in some instances used to hold elements of […]

  • Unique Number

    OVERVIEW We are going to write an algorithm that will detect the String to make sure that it is unique in […]

Leave a Reply

Your email address will not be published. Required fields are marked *