Reputation: 109
I'd like to pass my array from my first to my second ViewController. In the end of my first VC I tried to pass it like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondVC = ViewController2()
secondVC.passengers2 = passengers
print(secondVC.passengers2)
print(passengers)
}
"passengers" is my first array in "ViewController.swift" and "passengers2" my sewcond array in "ViewController2.swift".
When I go over to my second VC, the console tells me, that "passengers" and "passengers2" have the same value, but as soon as I am in "ViewController2.swift", "passengers2" is emtpy for some reason.
Does anyone know why?
Upvotes: 0
Views: 36
Reputation: 3362
The problem in your code is that you instantiate a new ViewController2 object, which is never used.
You want to use the destination view controller that is inside your segue object, like that :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destinationViewController as? ViewController2 {
destinationVC.passengers2 = passengers
}
}
Upvotes: 3