Pass Data with Delegation in Swift

Jayven N
iOS App Development
4 min readMar 8, 2017

0 to 100 Real Quick Project Example

I want to write an article that gets straight to the point. Like really straight to the point so that we can just look at this article and know how to pass data using delegation design patterns in our own projects.

Let’s go.

PREREQUISITES

Highly recommend that you first read about protocols if you haven’t. You don’t have to but it would probably help solidify your understanding.

Introduction to Protocols in Swift 3

THE WHEN

I am going talk about when we would want to pass data with delegation.

Think about prepareForSegue, then opposite direction. If prepareForSegue is pointing forward, then delegation is pointing backward.

Say prepareForSegue is from VCInitial to VCFinal, then delegation will be from VCFinal to VCInitial. This is when we would want to use delegation to pass data. It is also exactly what we are going to do right away!

SET UP

Open up a new Xcode project and call it whatever you want. Create two view controllers. Name the first one VCInitial and the second one VCFinal.

Should look something like:

Now embed VCInitial to a navigation controller. This way we have the back button on VCFinal.

Once embedded, should look something like:

Great. Now let’s create a segue between VCInitial and VCFinal. Call the segue VCInitialToVCFinal.

Now in our VCInitial. We want to perform a segue with our identifier. So create a button and connect it to an IBAction. Call the IBAction function btnPerformSeguePressed.

Look something like:

Then inside of our IBAction, perform segue with our identifier.

performSegue(withIdentifier: "VCInitialToVCFinal", sender: nil)

Now create a button for VCFinal and connect it to an IBAction. Call it btnPassDataPressed.

HALF TIME SHOW

Alright guys, welcome to the half-time show. We will recap the highlights of the night. Alright first thing first.

Two View Controllers and two buttons. Relationship.

Alright that’s the end of the half time show. We are moving to protocols now.

IMPLEMENT DELEGATION

Still on VCFinal.swift. Create a protocol at the very top. Best practice is to name the delegate protocol after your class name. After that we are going to add a required method func finishPassing(string: String).

Should look something like this:

protocol VCFinalDelegate {
func finishPassing(string: String)
}

Great. Our code is looking clean as fuck.

“That’s what I like.” — Bruno Mars

Now inside of our VCFinal class, we need to declare a delegate variable.

var delegate: VCFinalDelegate?

Then inside of btnPassDataPressed function, add the following code:

delegate?.finishPassing(string: "Sent from VCFinal")

This basically say do this for the lazy bum on button pressed. Our lazy bum is VCInitial. We will let VCInitial set itself as the lazy bum and hand off the task in just a bit.

Cool. Now go to the VCInitial.swift file and have VCInitial adopt our VCFinalDelegate protocol.

class VCInitial: UIViewController, VCFinalDelegate

Now we are going to safely unwrap our destination as VCFinal in our prepareForSegue function. Then set the destination’s delegate to self because we want VCInitial to be the receiver.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? VCFinal {
destination.delegate = self
}
}

Next we need to conform to VCFinalDelegate. Go ahead and add the finishPassing function inside of VCFinal.

func finishPassing(string: String) {
print("Notified")
}

DEMO

Go ahead and run the project.

Now we are going to see the received data from VCFinal.

Congrats fam, we killed it. 0–100 real quick.

LAST REMARKS

I hope you have enjoyed and learned something valuable from my article. If you have then let me know by hitting that ❤ button and follow me on Medium. Also, please share this article so that your circle can make some knowledge gains too.

For those interested, here is my LinkedIn.

Lastly if you have any comment, question, or recommendation, feel free to drop them below.

--

--