I'm trying to perform a segue between two table views via tapping on a cell. However it seems performSegue is loading the view before prepareForSegue has passed the data and thus the array is left empty in the second view controller.
Here's the code for my segue:
func prepare(for segue: UIStoryboardSegue, sender: Any?, didSelectRowAt indexPath: IndexPath) {
if let vc = segue.destination as? ModuleTableViewController {
vc.modules = programmes[indexPath.row].modules
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "ProgrammeToggleVCtoModuleVC", sender: self)
}
I'd like to have it pass across the data before the view is loaded, as there is a check in the second VC that causes the programme to crash if the data isn't present.
I've only been doing Swift for the past few weeks so excuse me if I've missed any info, happy to amend it if you need more information.
You need to hook segue from the vc itself not from the cell and correct prepare
to
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ModuleTableViewController {
vc.modules = programmes[tableView.indexPathForSelectedRow!.row].modules
}
}
OR
performSegue(withIdentifier: "ProgrammeToggleVCtoModuleVC", sender: indexPath.row)
with
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ModuleTableViewController {
vc.modules = programmes[sender as! Int].modules
}
}