DEV Community

Joshua Shinkle
Joshua Shinkle

Posted on

How to pass data between view controllers in swift

Background

A month ago, I was working on an iOS app that used a realtime database in Firebase to more efficiently store, access, and utilize data inputted by a user.

When storing and retrieving the user’s data, I needed to create a reference to my database.

In order to successfully create the reference, I needed to know the correct child name in my database in Firebase. The child name wasn’t constant though, so I couldn’t hardcode it.

I needed to assign the child name to a variable and pass the variable into my database reference.

This is where the problem occurred...

The Problem

For my project, I could only access the child name from my MainViewController and could only create my database reference in my SecondaryViewController.

Accordingly, I would need to pass the name from my MainViewController to my SecondaryViewController.

I researched several different methods and settled on using a segue to pass the child name between view controllers.

Here’s how I did it...

The Solution

First, in my MainViewController, I created a variable to access and store the child name.

let name: String = data.name

Second, in my SecondaryViewController, I created a variable for the child name. This is where the name would be stored when passed from the MainViewController to the SecondaryViewController.

var name: String = " "

Third, in my MainViewController, I defined the prepare(for:sender:) function which would be invoked before the segue between view controllers.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
}

Fourth, inside the prepare(for:sender:) function in my MainViewController, I used an if statement and optional casting to determine if the segue destination is the SecondaryViewController. I also defined a new variable called vc to give me access to my name variable in the SecondaryViewController.

if let vc = segue.destination as? SecondaryViewController {
}

Fifth, inside of the if statement in the MainViewController, I accessed the name variable in the SecondaryViewController and set it equal to the name variable in my MainViewController.

vc.name = name

That’s all! I’ve found it to be a pretty simple and effective method. Here’s what the full code looked like:

MainViewController:

let name: String = data.name
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if let vc = segue.destination as? SecondaryViewController
        {
            vc.name = name
        }
    }

SecondaryViewController:

var name: String = " "

In Closing

Here’s another resource I found helpful in learning to pass data between view controllers: https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/

The segue method worked for me but there might be a better way to do it! Leave a comment how you would attack this problem and let me know if you have an easier method!

Top comments (0)