-1

Hi I am new to programming in iOS. My question is how to move data from one view controller to another in Swift, specifically variables. Though there seems to be a lot of answers to be found online none of them address the answer to this specific in detail. Any help would be appreciated! Thanks. Edit: I finally realized how to use StackOverFlow better.This question could have been easily searched for. Sorry for any inconvenience.

TheBestCoder
  • 75
  • 1
  • 8

2 Answers2

4

You can add a property to the view controller you are moving to, and set that property with the data you want in the view you are moving from.

If you are instantiating and presenting your view manually, you can set the property directly on the instance of your new view. If you are using storyboards and segues, you can set the property in prepareForSegue().

A quick Google search finds this blog with a good example: http://www.codingexplorer.com/segue-swift-view-controllers/

Stephen
  • 1,098
  • 11
  • 27
1

If your view controller is embedded as the navigation controller, then you can use to push controller object to pass value like this:

let secondVC = self.storyboard.instantiateViewControllerWithIdentifier("secondVC") as! secondVC
secondVC.stringName = "Value"
self.navigationController.pushViewController(secondVC, animated: true)
James Taylor
  • 5,790
  • 7
  • 47
  • 66
Pratik Lad
  • 456
  • 4
  • 8