I would like to have a set up where info is passed between viewControllers as follows: 1->2->3->2. Currently I have a set up to pass info from VC
1->2. The info being passed is the indexPath that a cell is selected from a table. The info needs to be passed because the code in the 2nd viewController' viewDidLoad()
depends on the cell selected. It is set up as follows (and works fine):
VC 1:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "cellSelected" else {return}
let indexPath = self.tableView.indexPathForSelectedRow
let rowSelectedd = indexPath
let destViewController: MainPageCellSelectedViewController = segue.destination as! MainPageCellSelectedViewController
destViewController.rowSelected = rowSelectedd!
}
var rowSelectedd = IndexPath()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "cellSelected", sender: rowSelectedd)
}
VC 2 (using the info that is passed): var rowSelected = IndexPath() //this has info prepared for from the MainPageVC
override func viewDidLoad() {
super.viewDidLoad()
if rowSelected.section == 0{
//do something
} else{
// do something else
}
}
In this 2nd viewController there is a button that segues to a third viewController (this also works fine). However, in that third viewController there is a back button that segues back to the 2nd viewController. The problem is, the 2nd viewController's viewDidLoad()
code depends on the variable that was passed from the 1st VC. Since it isn't being passed (because the segue is now from 3->2 instead of 1->2) the app crashes. I am not sure what is the best way to transfer this info appropriately. How should it be done?