gpt4 book ai didi

ios - 如何将数据从 Collection View 单元格传递到新的 View Controller (以编程方式且无需 Storyboard)?

转载 作者:行者123 更新时间:2023-11-29 05:30:48 24 4
gpt4 key购买 nike

我在将数据从 Collection View 单元格传递到新 View Controller 时遇到问题。我有 Collection View 单元格,每个单元格都有图像和标签,我想为每个单元格创建新的 View Controller 。

我已经尝试过 UINavigationController 但目前和 navigationController 代码在 didSelectItemAt indexPath 函数下不起作用。每个人都用 Storyboard做到了这一点,但我没有使用它,而且我找不到解决方案。

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("#\(indexPath.item)!")
//let cell = collectionView.cellForItem(at: indexPath) as! itemsCell
//let cell = itemler[indexPath.row]

let newView = UINavigationController(rootViewController: orderBasketController())
self.present(newView, animated: true, completion: nil)
}

代码出现错误,类型“feedCell”的值没有成员“present”

最佳答案

尝试在 init 方法中注入(inject)依赖项以查看 Controller 。

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let yourCell = collectionView.cellForItem(at: indexPath) as? YourCellClass else {
return
}
let yourModel = Model(title: yourCell.title, image: yourCell.image)
let targetViewController = TargetViewController(model: yourModel)
navigationController?.pushViewController(targetViewController, animated: true)
// or if it's not in navigation controller
// present(targetViewController, animated: true, completion: nil)
}

关于ios - 如何将数据从 Collection View 单元格传递到新的 View Controller (以编程方式且无需 Storyboard)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57610592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com