[英]How can I passed data from collection view cell to new view controller (programmatically and without storyboard)?
I have problem about passing data from collection view cell to new view controller. 我有关于将数据从集合视图单元格传递到新视图控制器的问题。 I have collection view cells and each cell have image and labels and I want to create new view controllers for each cell.
我有集合视图单元,每个单元都有图像和标签,我想为每个单元创建新的视图控制器。
I already tried to UINavigationController but present and navigationController codes not working under didSelectItemAt indexPath function. 我已经尝试过UINavigationController,但是在didSelectItemAt indexPath函数下无法显示和navigationController代码。 Everybody did this with storyboard but I didn't use it and I can't find solution.
每个人都使用情节提要进行此操作,但我没有使用它,也找不到解决方案。
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)
}
Code had a error which is Value of type 'feedCell' has no member 'present' . 代码有一个错误,其错误类型为'feedCell'的Value没有成员'present' 。
Try to inject dependencies to view controller in init
method. 尝试在
init
方法中注入依赖项以查看控制器。
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.