Unwind Segue in Swift.. it’s not so hard!?

Why did I find it so hard to get my tiny mind wrapped around this simple concept!? A quick google search leads to some rather confusing information about how to implement an unwind segue.

So here is my attempt in a simple, easy to understand manner (I hope)

The key concept that I missed is where to place the @IBAction for the unwind segue. Using the storyboard editor I’ve been ctrl dragging my outlets and actions into the storyboards View Controller; it would stand to reason you do this for the exit as well?

You would be WRONG kind sir!

First, before you start playing with exit buttons and dragging all over the show only to get super frustrated that the exit button does ABSOLUTELY NOTHING for no apparent reason, you need to implement a method which takes an argument of UIStoryboardSegue in the destination view controller — ie; the controller you wish to unwind to.

Link up exit

Now that you have that in place, you can wire up you ‘exit button’ to the exit icon by ctrl + dragging up to the exit icon. You will be presented with a selection of segue handlers. Select unwindSegue (assuming that’s what you’ve called it).

Now in the document outline window, select Unwind segue to Scene Exit Pla… and give the segue an identifier, something like ‘toHomeView’. You’ll use this identifier in the View Controller that you are segueing from.

Now that you have the segue wired up you can now perform the segue. Open up the ‘from’ view controller. You need to override the prepareForSegue method:

You should now have a working unwind segue. Really not much too it, but understanding where to put each bit took me a morning to work out. Hopefully this helps you out!

Like what you read? Give Jamie Le Souëf  a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.