【问题标题】:Swift Navigate from UICollectionViewCell to UIViewControllerSwift 从 UICollectionViewCell 导航到 UIViewController
【发布时间】:2018-06-19 17:01:05
【问题描述】:

我有麻烦了!我需要以编程方式设置从 UICollectionViewCell (EVRecordingDetailsCollectionViewCell) 到 UIViewController (EVPersonalDataViewController) 的导航。

我试过这种方式..但它说 CollectionViewCell 没有成员“存在”

let showloginViewController = EVPersonalDataViewController()
        let ncShowloginViewController = UINavigationController(rootViewController: showloginViewController)
        self.present(ncShowloginViewController, animated: true, completion: nil)

【问题讨论】:

    标签: swift navigation


    【解决方案1】:

    函数 present(UIViewController, Bool, (() -> Void)) 是 UIViewController 的成员,而不是 UICollectionViewCell。

    这样用

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "yourCellId", for: indexPath) as! CustomCell
        cell.parentViewController = self
        // your cell setup...
        return cell
    }
    

    在你的单元格类中保持对你的 UIViewController 的弱引用

    class CustomCell: UICollectionViewCell {
    
        weak var parentViewController: UIViewController?
    
        @IBAction func onClickButton(sender: UIButton) {
            let showloginViewController = EVPersonalDataViewController()
            let ncShowloginViewController = UINavigationController(rootViewController: showloginViewController)
            parentViewController?.present(ncShowloginViewController, animated: true, completion: nil)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-05
      • 1970-01-01
      • 2020-06-19
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多