我希望能够在DetailViewController.
中重新加载值。如何对所选的indexPath进行预设,以便在下一个视图控制器中调用该值?
在didSelectItemAt
我有:
let monsters: Monsters!
let mons: [Monsters] = Data().mons
monsters = mons[indexPath.row]
self.performSegue(withIdentifier: "DetailVC", sender: monsters)
答案 0 :(得分:3)
您可以在monster
中创建DetailViewController
媒体资源,然后在致电prepare(for segue:)
// This would be a property at the top of DetailViewController
var monster:Monster?
然后在执行segue的VC内部,为selectedMonster
创建一个基于indexPath
分配的属性:
var selectedMonster:Monster?
然后当您用户选择一个单元格时:
selectedMonster = mons[indexPath.row]
在同一个VC中,调用performSegue(withIdentifier:)
时会调用此方法:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "DetailVC" {
let detailVC = segue.destination as! DetailViewController
detailVC.monster = selectedMonster
}
}
您可能想要查看performSegue(withIdentifier:)
的来电,通常您希望发件人为self
,即:
performSegue(withIdentifier: "DetailVC", sender: self)