【发布时间】:2021-09-06 11:06:52
【问题描述】:
我在ViewController 内有一个CollectionView,它不会对第二个ViewController 执行segue
我的代码如下:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "showFood" else { return }
guard let destination = segue.destination as? FoodViewController else { return }
destination.food = sender as? Food
}
extension AllFoodViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showFood", sender: food[indexPath.row])
}
另外,我在 ViewDidLoad 中有这个功能
private func configureCollectionView() {
collectionView.delegate = self
collectionView.dataSource = self
}
【问题讨论】:
-
我假设您在该特定视图控制器的 viewDidLoad() 上调用了 configureCollectionView()?假设您这样做了,将保护语句更改为“if let”语句可能会有所帮助,因此您可以在代码中添加一些调试打印。这样你就可以看到你是否甚至超过了代码的这些部分。
-
是的,我在那个 ViewController 上调用了函数
-
然后我会看一下以确保您的 segue 配置正确(确保在输入标识符后点击“return”.. 过去没有这样做让我绊倒了! )。从您发布的内容来看,代码看起来应该会触发,但如果您只是在这些保护语句上调用“return”,您将错过一些关键的调试步骤。
-
我在 segue 上设置了断点,但它不起作用,所以应用程序无法识别我的点击。我也在 iPhone 上试过,结果是一样的
-
在设计中显示segue连接
标签: ios swift uicollectionview uicollectionviewcell collectionview