0

How do I pass the data from an array of one view controller to another view controller?

FirstViewController:

var myArray = ["some data"]

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
  let destinationVC = segue.destination as! SecondViewController
  destinationVC.passedArray = myArray
}

SecondViewController:

var passedArray = [String]()

override func viewDidLoad(){
  super.viewDidLoad()
  print(passedArray)
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
Juan Jose Rodrigo
  • 429
  • 2
  • 6
  • 14
  • 1
    I see a syntax issue. It should be `override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {` ...from http://stackoverflow.com/a/24040979/354144 – Neal Ehardt Jan 13 '17 at 04:51
  • 1
    @NealEhardt Signature of `prepare(for:sender:)` is changed in Swift 3. – Nirav D Jan 13 '17 at 04:55
  • Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Suragch Apr 16 '18 at 00:24

1 Answers1

0

You can try this Solution for Data Transfer.

In FirstViewController.swift

let str = "Hello It's Done"
func callsecondViewController()
    {
        let controller = storyboard?.instantiateViewController(withIdentifier: "second") as! secondViewController
        self.present(controller, animated: true, completion: nil)
    }
func temp() -> NSString 
    {
        return str as NSString
    }

in SecondViewController.swift

var data = [NSString]()
    override func viewDidLoad() 
    {
        super.viewDidLoad()
        let view = ViewController()
        data = [view.temp()]
        print(data)
    }

and you get the output String as Hello It's Done

Dhruv Khatri
  • 803
  • 6
  • 15
  • You can either present of use `self.navigationController?.pushViewController(controller, animated: true)` , but make sure you've navigation controller in the stack. – anuraagdjain Jan 13 '17 at 06:41