繁体   English   中英

如何将数据从集合视图单元传递到新的视图控制器(以编程方式且没有情节提要)?

[英]How can I passed data from collection view cell to new view controller (programmatically and without storyboard)?

我有关于将数据从集合视图单元格传递到新视图控制器的问题。 我有集合视图单元,每个单元都有图像和标签,我想为每个单元创建新的视图控制器。

我已经尝试过UINavigationController,但是在didSelectItemAt indexPath函数下无法显示和navigationController代码。 每个人都使用情节提要进行此操作,但我没有使用它,也找不到解决方案。

    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'的Value没有成员'present'

尝试在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM