簡體   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