我有一个由图像数组填充的 UICollectionView
。我想让用户点击图像并在新的 UIViewController
中预览它。
由于某些原因,didSelectItemAt
方法未按预期工作。它不会抛出错误,它会被调用,只是什么也没发生。不确定我在这里做错了什么。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("did select item at was called")
let dashboardStoryboard = UIStoryboard(name: STORYBOARD_DASHBOARD, bundle:nil)
let destinationVC = dashboardStoryboard.instantiateViewController(withIdentifier: "PreviewPostImageVC") as! PreviewPostImageVC
destinationVC.selectedImage = arrayOfSelectedImages[indexPath.row]
self.navigationController?.pushViewController(destinationVC, animated: true)
}
最佳答案
我认为 Allen 的评论指出了问题所在。如果您确定正在调用您的方法(打印语句出现在控制台中),那么最有可能的问题是 self.navigationController
为 nil。使用您正在使用的可选链接,如果它为 nil,则该调用将静默失败,这可能是您想要的,也可能不是您想要的。尝试像这样更改它:
guard let navController = self.navigationController else {
print("Navigation Controller is nil!")
return
}
navController.pushViewController(destinationVC, animated: true)
关于ios - UICollectionView didSelectIotemAt issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45195759/