1

I want to know how do you pass data, like values, from a second view controller into the first view controller. I did a simple segue from the first to the second. I just don't know how to do it backwards.

I have a view on my first controller, and a view on the second one. I want to pass the changes from the second view into the view on the first controller when I click on a custom back button I made. How do I go about doing that?

I know it has something to do with unwind segue, but everything I look up is very unclear and confusing.

Nyxx
  • 303
  • 3
  • 13
  • See the "passing data back" in http://stackoverflow.com/a/9736559/1271826. Or see unwind segue references in other answers there. – Rob Jul 09 '16 at 15:14

1 Answers1

0

To do it backwards, you could do it with delegate or blocks like what @Rob linked in the comments.

You can also do a unwind segue like what you mention. Just like a simple segue, unwind segue could help you pass data from the second view controller to first view controller in prepareForSegue.

Example you have a ViewControllerA that segue to the ViewControllerB. This time you want to setup the unwind segue for ViewControllerB to ViewControllerA. Before setting up on storyboard, you will need to setup the @IBAction in ViewControllerA the UIViewController you want to unwind to not ViewControllerB. Something like this:

@IBAction func cancelToViewControllerA(segue:UIStoryboardSegue) {
}

In storyboard, at the ViewControllerB control drag the button that will call the unwind segue to the exit and select cancelToViewControllerA. Then give it a name for the unwind segue. In ViewControllerB override prepareForSegue like what you did for passing data forward to set the data for the unwind segue.

Zac Kwan
  • 5,587
  • 4
  • 20
  • 27
  • in your last sentence you say, " In ViewControllerB override prepareForSegue like what you did for passing data forward to set the data for the unwind segue." can explain that part a little further? how do you override the prepareForSegue routine in order to pass data back similarly to the way it was passed forward? – lopezdp Oct 29 '16 at 00:28