How to pass data between iOS ViewControllers in Swift – XSTutorials

How to pass data between iOS ViewControllers in Swift

Most of the times, we have to call and use variables in different ViewControllers, they may be Strings, Numbers, Booleans, etc. In this article, you’ll learn the two common ways to store and reuse data between screens.

Use Global Variables

One of the greatest features about Swift is that you can instantiate variables outside the Class declaration of a Swift file and call them in other files. Let’s pretend you have a blank Xcode project, add these variables on the top of your ViewController.swift file, between the imports and the Class declaration:

let myString = "Hello world"
let myInt = 123
let myBool = true

Paste this code inside the viewDidLoad() function – it will modally show a Second Controller as soon as the first one gets loaded:

let vc = storyboard?.instantiateViewController(withIdentifier: "SecondVC") as! SecondVC
present(vc, animated: true, completion: nil)

Now add a new ViewController in the Storyboard and a new Swift file, call it “SecondVC”, save it and assign its name to the Controller in the Identity inspector – remember to also set the Storyboard ID.

Declare the Second ViewConroller

Inside the viewDidload() function of the SecondVC.swift file, paste this piece of code:

print(myString)
print(myInt)
print(myBool)

Run the app and check the Xcode console, you should get the following data:

Hello world
123
false

So, declaring global variables in any Swift file, outside the Class, will ake you able to retrieve them from any other file of your project.

NOTE: if you initialized those variables with the var prefix, you could edit them in any Swift file. In this case, they are immutable because of their let prefix.

Store variables inside a Class

Let’s move out 3 variables inside the ViewController class declaration:

class ViewController: UIViewController {
   
    let myString = "Hello world"
    let myInt = 123
    let myBool = true
    ...

Clean the project now – click Product -> Clean Build Folder. Xcode will highlight some code in the SecondVC.swift file and mark it as an error.

Errors on the variables

This happens because our variables are no longer global, so Xcode cannot recognize them from a different file than the ViewController.swift one. So we have to pass our variable from that file to the SecondVC.swift one.

Edit the lines of code in the viewDidLoad() method of the ViewController.swift as follows:

let vc = storyboard?.instantiateViewController(withIdentifier: "SecondVC") as! SecondVC
        
vc.myPassedString = myString
vc.myPassedInt = myInt
vc.myPassedBool = myBool
        
present(vc, animated: true, completion: nil)

Switch to the SecondVC.swift file and add the following variables inside the Class declaration:

var myPassedString = ""
var myPassedInt = 0
var myPassedBool = false

Now edit the code inside the viewDidLoad() function like this:

print(myPassedString)
print(myPassedInt)
print(myPassedBool)

The error lines will disappear because we told the code to print the local variables we’ve just declared. Run the app and check the console out, it should print the following messages:

Hello world
123
true

Let’s try a nice experiment: edit those variables after retrieving them from the ViewController.swift file. This is the code you have to place above the print instances in the SecondVC.swift file:

myPassedString += ", how is it going?"
myPassedInt = myPassedInt + 7
myPassedBool = false

Run the app again and read the Xcode console log, this is what you’ll see:

Hello world, how is it going?
130
false

Conclusion

That’s all for this tutorial, you have learned how to pass data between Swift files, and also how to edit passed variables in the receiver file.

Hope you enjoyed this article, feel free to post comments about it. 

Buy me a coffee - XScoder - thanks for your support
Your support will be highly appreciated 😉