I want to be able to reload the value in my DetailViewController.
How do I segue the selected indexPath so I can call the value in the next view controller?
In my didSelectItemAt
I have:
let monsters: Monsters!
let mons: [Monsters] = Data().mons
monsters = mons[indexPath.row]
self.performSegue(withIdentifier: "DetailVC", sender: monsters)
You could create a monster
property in DetailViewController
, and then assign it when you call prepare(for segue:)
// This would be a property at the top of DetailViewController
var monster:Monster?
Then inside the VC that performs the segue, create a property for selectedMonster
to assign based on indexPath
:
var selectedMonster:Monster?
Then when you user selects a cell:
selectedMonster = mons[indexPath.row]
In this same VC, this method would be called when the performSegue(withIdentifier:)
gets called:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DetailVC" {
let detailVC = segue.destination as! DetailViewController
detailVC.monster = selectedMonster
}
}
You may want to check how you call performSegue(withIdentifier:)
, usually you want the sender to be self
, i.e. :
performSegue(withIdentifier: "DetailVC", sender: self)