ios - UICollectionView didSelectIotemAt issue

标签 ios swift uicollectionview

我有一个由图像数组填充的 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/

相关文章:

ios - Xamarin 表单 iOS WebRTC

ios - Objective-C:使用 UNUserNotificationCenter 设置计时器以显示提醒

ios - Swift:如何确保 seque 在 http 任务完成之前不运行?

ios - 如何使用 uiwebview 和 swift 登录网站?

iphone - 将 UICollectionViewCell 的原点转换为 ViewController 的框架

ios - UICollectionViewLayout(从 Swift 转换为 Objective-C)

ios - swift 展开问题

ios - 对象层次结构导致 Swift 内存泄漏

ios - 使用 UISearchDisplayController 时,UICollectionView 中的 UISearchBar 消失

ios - Cocoapod 在 pod install 时出错,没有生成 xcworkspace