Pass Information Forward From One ViewController to Another in Swift

To learn how to pass data back to the previous view controller read this blog post.

To illustrate how to pass information forward from one view controller to another view controller in Swift, I will create a project with two views on the Main.storyboard. Let’s follow these steps:

  1. Create a new Xcode project if do not already have it. I will create a new one for this particular example
  2. Open Main.storyboard and drag and drop on it a new view controller. In my case, I already had one created by Xcode, so to make it two, I needed to drag only one view controller.
  3. For the second view controller on Main.storyboard create a new Swift file with a name SecondViewController.Swift and make sure it is a subclass of UIViewController. And I will set its Identity->Storyboard ID to SecondViewController.

So my Main.storyboard will now look like this:

1)-StoryBoard-with-two-controllers

 

Passing String value forward to SecondViewController

Now when we have two View Controllers let’s try a very simple example first. We will pass a String value from one View Controller to another when user taps on a button. Let’s create a button first.  

  1. Open Main.storyboard
  2. Select first ViewController and from the Objects library drag and drop UIButton and UITextField on the View
  3. Open Assistant Editor and create for the Button an Action Outlet with the name myButtonTapped.
  4. While Assistant Editor is opened create an outlet for UITextField with a name myTextField
  5. For our convenience let’s also embed first ViewController into Navigation Controller. We do that by selecting the first ViewController on the Main.storyboard and then from the top menu select Editor -> Embed in -> Navigation Controller. Navigation Controller will let us have the Back button which we can use to go back from SecondViewController to first ViewController.

Now when user taps on our button a function with a name myButtonTapped will be called. Here is how my screen looks now:

2)ButtonWithAnAction
Let’s assume we need to pass to our SecondViewController a String value “Hello World”. Here is what we will do.

Inside of Second View Controller:

  1. Create a property of type String with a name myStringValue
  2. To keep this example simple we will simply print the value of myStringValue property inside the viewWillAppear function. The viewWillAppear function will be called automatically every time when SecondViewController is about to appear on the screen.

Here is how my SecondViewController now looks:

import UIKit

class SecondViewController: UIViewController {
    
    var myStringValue:String?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // We will simply print out the value here
        print("The value of myStringValue is: \(myStringValue!)")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

 

Inside of our myButtonTapped function which is in first ViewController:

  1. We will write code to check if myTextField is not empty
  2. Create an instance of SecondViewController and set the value of its variable myStringValue to the value from myTextField
  3. And finally we will Present(push) SecondViewController to user.

Here is how my first ViewController and it’s myButtonTapped function now looks:

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var myTextField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func myButtonTapped(sender: AnyObject) {
        
        // Check if value from myTextField is not empty
        if myTextField.text?.isEmpty == true
        {
            return
        }

        // Instantiate SecondViewController
        let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
            
        // Set "Hello World" as a value to myStringValue
        secondViewController.myStringValue = myTextField.text
            
        // Take user to SecondViewController
        self.navigationController?.pushViewController(secondViewController, animated: true)
   
    }
}

Now run your application, type Hello World into UITextField and tap on a button to see how it works. If the text field is not empty, user will be taken to a SecondViewController and the value set to myStringValue will be printed out to Debug area. Here is my screenshot:

3)HelloWorldPrinted

 

Here is a video tutorial that demonstrates how to pass information from a ViewController with a UITableView to another ViewController with a UIWebView.

 

Next I will show you how to pass information back from SecondViewController to first ViewController.