How to Pass Data in an Unwind Segue (Swift 3)

Erica Millado
Yay It’s Erica
Published in
4 min readJun 13, 2017
Two view controllers and an unwind segue

An unwind segue (aka an “exit segue”) is a segue that allows a user to navigate back from their current view controller to a previous view controller.

Imagine: I have two view controllers and viewController1 segues to viewController2. The user creates or edits data in viewController2 and I want to send this data back to viewController1. An unwind segue can help with this.

To illustrate how I used an unwind segue to pass data between two viewControllers, I made an app that lists favorite Mad Men characters.

Segue: MadMenTableViewController -> AddCharacterViewController

The first view controller, MadMenTableViewController, displays a tableView of characters. A “+” button in the MadMenTableViewController’s navigation bar segues a user to the AddCharacterViewController where a user can enter a favorite character’s name into a textField and push a “Save” button in its navigation bar.

Unwind Segue: AddCharacterViewController -> MadMenTableViewController

When I “unwind” the segue from AddCharacterViewController to take the new character’s name (a string from the textField) back to the MadMenTableViewController, I want to be able to pass this character name with the unwind segue so that it can be added to the array of Mad Men characters and displayed in the table.

Storyboard Setup

In Storyboard, from my MadMenTableViewController, I control-dragged my navigation “+” (add) button to the AddCharacterViewController view and chose “Show” (e.g. Push) as its segue type.

AddCharacterViewController Setup

In my AddCharacterViewController, I have a stored property called character that will hold the name of the new character entered by the user (from the textField). I override the prepare(for segue:) method and unwrap the text from the textField and assign its value to the character property. It is in this function that the character value will be passed back to the MadMenTableViewController when the segue is unwound.

MadMenTableViewController Setup

In my MadMenTableViewController class, I write an @IBAction function, unwindFromAddVC(_ sender:) that will be called when the AddCharacterViewController unwinds the segue (when the “Save” button is clicked and the AddCharacterViewController is exited.

Above, on line #21 I have my unwindFromAddVC function. This function is what is going to receive the character name string and add it to the array of existing Mad Men favorites.

On line #23, I check to see if the source that is unwinding the segue is my desired AddCharacterViewController.

On line #24, if it is indeed my AddCharacterViewController, I unwrap this senderVC and access its character (String) property (remember, this string was holding our character’s name).

On line #25, this character property is appended to our madMenCharacters array.

On line #27, I make sure to refresh the tableView since we now have a new item in our array that needs to be displayed.

Connecting “Save” to “Exiting the AddCharacterViewController”

Back in Storyboard, there’s one last thing to do.

Control drag the “Save” button in the AddCharacterViewController to its Exit Icon (orange icon on far right). When you let go, the @IBAction method in the MadMenTableViewController should appear. Select it, build and run your app.

The data from my second view controller is now passed to my first view controller! It was pretty easy!

Yup, it was really this easy.

Check it out:

My repo for this project can be found here.

Resources:

Using Segues — Apple Documentation

--

--