In contrast to segue that lets you pass data "forward" from current view controller to destination view controller:
(VC1) -> (VC2)
Using "unwind" you can do the opposite, pass data from the destination or current view controller to its presenting view controller:
(VC1) <- (VC2)
NOTE: Pay attention that using unwind lets you pass the data first and afterwards the current view controller (VC2) will get deallocated.
Here's how to do it:
First, you will need to add the following declaration at the presenting view controller (VC1) which is the view controller that we want to pass the data to:
@IBAction func unwindToPresentingViewController(segue:UIStoryboardSegue)
The important thing is to use the prefix unwind
, this "informs" Xcode that this is an unwind method giving you the option to use it in storyboard as well.
Afterwards you will need to implement the method, it looks almost the same as an actual segue:
@IBAction func unwindToPresentingViewController(segue:UIStoryboardSegue)
{
if segue.identifier == "YourCustomIdentifer"
{
if let VC2 = segue.sourceViewController as? VC2
{
// Your custom code in here to access VC2 class member
}
Now you have 2 options to invoke the unwind calls:
self.performSegueWithIdentifier("YourCustomIdentifier", sender: self)
which will do the unwind for you whenever you will performSegueWithIdentifier
.storyboard
to your "Exit" object: ctrl + drag the button you want to invoke the unwind method, to the "Exit" object:Release and you will have the option to choose your custom unwind method: