简体   繁体   English

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

[英]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.

相关问题 如何在不使用情节提要或IB的情况下从嵌入式集合视图的单元导航到另一个视图控制器? - How to navigate from an embedded collection view’s cell to another view controller without using Storyboard or IB? 如何从情节提要中的子视图(另一个视图控制器)内的按钮推动新的视图控制器 - How can I push a new View Controller from a button within a subView (another View Controller) in Storyboard 如何将数据数据从集合视图单元传递到视图控制器 - how to pass data data from collection view cell to view controller 将数据从集合视图单元格移动到新的集合视图单元格 - Moving data from a collection view cell to a new collection view cell 当我轻按任何集合视图单元格时(不点击最后一个单元格),如何移动另一个视图控制器? - How to move another view controller when i tapped any collection view cell (without taping last cell)? 如何从不在情节提要中但以编程方式创建的视图控制器呈现位于“主”情节提要中的视图控制器? - How to present a view controller that is in the “main” storyboard from a view controller that isn't in the storyboard but is created programmatically? Swift 4:将数据从集合视图单元格传递到另一个没有故事板的视图控制器 - Swift 4: Passing data from a collection view cell into another view controller without storyboards 使用故事板中的选项卡控制器以编程方式打开视图 - Open a view programmatically with tab controller from storyboard 使用AppDelegate中的Storyboard以编程方式实例化View Controller - Instantiating a View Controller programmatically with Storyboard from AppDelegate 将数据从集合视图单元传递到视图 Controller - Passing data from Collection View Cell to View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM