Passing data between ViewControllers via Delegate & Protocols

We all know how to pass data between ViewControllers via the prepareForSegue method. It has it’s own limitations and advantages. The good news is, it’s not the only way you can pass data between ViewControllers.

One of the most popular and common methods to pass data between ViewControllers is via the Delegate and Protocols. Before starting this tutorial, if you haven’t already, please read my article on Protocol. If you understand Protocol then this article would make more sense.

Instead of talking too much let’s dive into an example and see how it’s done.

Let’s create a Single Page Application named “Spy and Base”.

Now delete the default ViewController.Swift and create two more ViewControllers named “HomeBaseVC” and “EnemyBaseVC”

After creating the ViewControllers, it should look like below.

It’s time to do some changes in the StoryBoard. Let’s open storyboard and change the Class name of the existing ViewController as “HomeBaseVC”.

Now it’s time to add another Viewcontroller to the storyboard and associate it with the ViewController class “EnemyBaseVC”

It’s time to connect both VCs via segue. We’ll add a UIButton to “HomeBaseVC” named “SEND OUR SPY TO ENEMY BASE”. Upon tapping this button we’ll be taken to “EnemyBaseVC”. (I’ll leave the AutoLayout constraints part as it’s not part of the tutorial)

We’ll show the EnemyBaseVC as Modal (aka a popup window). Also make sure the segue identifier as “sendSpySegue”. Look at the gif below on how it’s done.

Cool! Now we can send a spy to Enemy Base from our Home Base. Let’s say our spy infiltrated the enemy base. So it’s time to give him a UIButton which will allow him to hack enemy communication system and pass data back to Home Base.

So I’m gonna add a UIButton to the “EnemyBaseVC” as “HACK AND SEND MESSAGE HOME”

So the storyboard should look like something below.

Before leaving the storyboard, let’s connect the “HACK AND SEND MESSAGE HOME” button and create an IBAction.

Time to set up the “EnemyBaseVC” with data. I’ll update the VC as below. I hope it’s self explanatory. Also please note that I’ve removed all the boilerplate code from the VC to improve the readability.

It’s time to put some basics we learned from protocol lesson into practice. I’m gonna declare a protocol as below.

Now time to create a delegate instance of SpyDelegate in the “EnemyBaseVC” as below.

Using this delegate instance we gonna send data back to HomeBaseVC via the sendingMessage IBAction. Check out how it’s done below.

Now the infrastructure is ready to send message from “EnemyBaseVC” but we didn’t setup the infrastructure in our “HomeBaseVC”. Time to do it too.

First “HomeBaseVC” must conform to “SpyDelegate” and implement the method “didFindWeaponOfMassDestruction”

Oh yeah the things you learned at our protocol tutorial are put in practice above.

Ideally when the data is sent from the “EnemyBaseVC” it should be received by the “” method in “HomeBaseVC”. Let’s run the app and see whether it works.


still it won’t work for two reasons

  • Still the delegate variable in “EnemyBaseVC” is nil
  • delegate doesn’t know yet to which VC it should send the data back.

Let’s patch the above mentioned issues in “prepareForSegue” method as below.

Now the “delegate” variable is not nil anymore and it also knows that it should pass the data back to “HomeBaseVC” because we assigned “self” (which is HomeBaseVC in this context) to delegate.

Now run the app and see whether data being sent back.

Cool! works yeah? Awesome let’s do some final adjustments.

First dismiss the EnemyBaseVC when the message is sent.

Do some modifications in the didFindWeaponOfMassDestruction function.

Awesome right? Now change the bool value “hasWeaponOfMassDescrtruction” to both true and false and see how the data being sent.

This simple example shows another way how the data can be passed between ViewControllers. I tried making it as simple as possible. Hope you learned something out of it.

Download the final project from GitHub.