I created two view controllers. I created a segue from the first to the second to pass data. Now I want to pass data from the second view controller to the first one. I went through many similar questions and I'm not able to implement those as I lack the knowledge on how unwinding works.
ViewController.swift
class ViewController: UIViewController
{
var dataRecieved: String?
@IBOutlet weak var labelOne: UILabel!
@IBAction func buttonOne(sender: UIButton)
{
performSegueWithIdentifier("viewNext", sender: self)
}
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
{
var svc: viewControllerB = segue.destinationViewController as! viewControllerB
svc.dataPassed = labelOne.text
}
}
This will pass the data to dataPassed in view controller "viewControllerB". Say, now I want to pass some data from viewControllerB to dataRecieved in ViewController. How can I do this with only unwind segue and not by using delegate. I'm quite new to swift, would appreciate a detailed explanation.
This is how I would do it:
Create an outlet in view controller 1, like this:
Connect view controller 2 (the vc you are unwinding from) like shown below. Drag from the yellow circle in vc2 to 'Exit'. The IBAction from view controller 1 should pop up. Select it.
Now, whenever you unwind from view controller 2, the
unwindToViewController1:
method in view controller 1 will get called.This is where you'll retrieve the property you want from view controller 2. Note that you need to cast the
segue.sourceViewController
to your custom view controller subclass in order to get the right property.Øyvind Hauge beat me to the same solution method, but as I had already started with a more detailed answer, I'll add it as well.
Let's say your two view controllers are named as follows:
ViewController
(vcA)
ViewControllerB
(vcB)
You set up the segue from
(vcA) -> (vcB)
as you have done in you exampleThe somewhat tricky step next is that, using this method, the segue used for passing data back from
(vcB)
to(vcA)
is also added to the source of(vcA)
, as an@IBAction
method (rather than, as could possibly be expected, added to the source of(vcB)
).You thereafter connect say, a button in
(vcB)
to this unwind action in(vcA)
via the manualExit
segue in(vcB)
:Below follows a complete example of passing text from
(vcA)
to(vcB)
; (possibly) modifying that text via anUITextField
, finally returning the (possibly) modified text to(vcA)
.(vcA)
source:(vcB)
source (note that theUITextFieldDelegate
delegate here is only used for "locally" mutating the value of thedataPassed
property, which will be returned to(vcA)
and assigned todataRecieved
property of the latter)Example execution:
If your app supports iOS 9+ you can pass data almost the same as prepareForSegue, use UIStoryboardUnwindSegueSource that has a sender property which is exactly the same as the the
sender
property in prepare(for segue: UIStoryboardSegue, sender: Any?).How to use it:
Note: Connecting the unwindTo method is the same as @Øyvind Hauge and @dfri explained in their answers.
fromViewController
is the type you were coming fromsender
property to the type you sent and return trueCode snip (Swift 4.0):