Passing data backwards using unwind to segue

suggest change

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:

  1. You can “hard code” invoke the: self.performSegueWithIdentifier("YourCustomIdentifier", sender: self) which will do the unwind for you whenever you will performSegueWithIdentifier.
  2. You can link the unwind method using the 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:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Passing Data between View Controllers:
* Passing data backwards using unwind to segue

Table Of Contents
3 Passing Data between View Controllers
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable