1

I have two ViewControllers. One that is the main menu of my game, called MenuViewController, and another one which is the level you play in, called ViewController. To get to the level, from the menu, I use a segue. To go back to the menu from the level I use dismissViewControllerAnimated, like this:

func leaveScene() {
    self.dismissViewControllerAnimated(true, completion: nil)
}

What I wanted to know is, I have a variable called x for example, and I want to pass its value to the MenuViewController so I can access it there. How can this be done?

Shamas S
  • 7,507
  • 10
  • 46
  • 58
Pedro Cabaço
  • 123
  • 12
  • Just use an unwind segue, then you can access the ViewController there – Dreaming In Binary Jun 26 '15 at 15:53
  • Can you please show some code or an example for this case? – Pedro Cabaço Jun 26 '15 at 15:54
  • 1
    possible duplicate of [dismissModalViewController AND pass data back](http://stackoverflow.com/questions/6203799/dismissmodalviewcontroller-and-pass-data-back) – JAL Jun 26 '15 at 15:55
  • Yes but it's in objective c and I want a simpler way to do this, in Swift! – Pedro Cabaço Jun 26 '15 at 16:00
  • Ok I found out the solution, but it doesnt work for my case. In my app, to dismiss the view controller, I swipe... How can I do the unwind segue with swipe? – Pedro Cabaço Jun 26 '15 at 16:08
  • Here is how to set up an unwind segue to be called programmatically. Then just call it from your swipe handling routine. http://stackoverflow.com/a/25202589/1630618 – vacawama Jun 26 '15 at 16:26
  • Great, it's working! Now how do I access my viewController from there? – Pedro Cabaço Jun 26 '15 at 16:34
  • `let sourceViewController = segue.sourceViewController as! MyViewControllerType`. the "source" view controller is the one you are returning from. Cast the value to the type of the viewController and then you can access its properties. – vacawama Jun 26 '15 at 16:44
  • That goes in your unwind segue landing function. – vacawama Jun 26 '15 at 16:47
  • You have an answer in Swift here: http://stackoverflow.com/questions/24318480/passing-data-in-swift – Marie Dm Aug 26 '15 at 12:19

0 Answers0