I have two View Controller, lets call them A1 and A2. A1 has consits of a tableView with various entries. If I click on an entry namend "Orange" in A1, A2 should show details about Orange. To store the data I use Core Data. I filter the data I need for the Orange in A1 and now I want to pass the data like name, size etc. to A2. I tried to use a function in A1 wich sets the variables in A2. This works but when I perform the segue all the data is gone. What can I do to pass the data?
I call this initializer function from A1, the function itself is placed in A2.
Code Block func initializer(caption: String, size : String, text: String) { self.text = text self.fruitCaption = caption self.fruitSize = size }
I call this initializer function from A1, the function itself is placed in A2.
I fear you are doing it improperly.
you create
but the segue will instantiate another ThirdViewController.
And you loose everything you have passed to vc which is deleted when exiting the func.
So you should:
you create
Code Block let vc = ThirdViewController()
but the segue will instantiate another ThirdViewController.
And you loose everything you have passed to vc which is deleted when exiting the func.
So you should:
define a prepare for segue func
include the preparation code in it, but NOT create instance of ThirdViewController
just call if let vc = segue.destination
Code Block override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "SegueID" { if let destVC = segue.destination as? ThirdViewController { destVC.caption = caption // Do the same with all properties destVC.fruitCaption = caption destVC.fruitSize = size } } }