[英]Swift How to segue indexPath value from CollectionviewCell?
I want to be able to reload the value in my DetailViewController.
我希望能够在我的
DetailViewController.
重新加载值DetailViewController.
How do I segue the selected indexPath so I can call the value in the next view controller? 如何选择选定的indexPath,以便可以在下一个视图控制器中调用该值?
In my didSelectItemAt
I have: 在我的
didSelectItemAt
我有:
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:)
您可以在
DetailViewController
创建一个monster
属性,然后在调用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
: 然后在执行segue的VC中,为
selectedMonster
创建一个属性,以基于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: 在同一VC中,当
performSegue(withIdentifier:)
被调用时,将调用此方法:
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
, ie : 您可能需要检查如何调用
performSegue(withIdentifier:)
,通常您希望发送方是self
,即:
performSegue(withIdentifier: "DetailVC", sender: self)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.