【问题标题】:How to move from one view controller to another view controller though dynamic collection view using swift 5?如何通过使用 swift 5 的动态集合视图从一个视图控制器移动到另一个视图控制器?
【发布时间】:2021-04-01 09:28:25
【问题描述】:

screenshot of image slider

我有一个动态集合视图。如何通过单击集合视图的单元格从一个视图控制器移动到另一个视图控制器? 提前谢谢你

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    collectionview有一个委托方法didselect可以使用

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // write code to go from one viewcontroller to another
    }
    

    【讨论】:

    • 在你的collectionview的委托下面添加给定的方法(collection view的代码)
    【解决方案2】:

    确保您已将 collectionView 授予委托。请参阅下文如何设置委托。

    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionvVew.delegate = self
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // Write your navigation controller name instead of **ViewController**
        let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "ViewController") as? ViewController
        self.navigationController?.pushViewController(vc!, animated: true)
    }
    

    我已经分享了我的答案。如果我对您的问题的理解有误,请告诉我。

    【讨论】:

    • 感谢您的反馈,但此代码不起作用。它不再改变视图控制器。
    • @Abhi 如果您可以分享您的控制器代码以便更好地理解?
    • 您能否分享您的电子邮件 ID,以便我可以发送控制器中使用的总代码,否则它太长了
    【解决方案3】:

    尝试将手势识别器(在您的 cellForItemAt 函数中)添加到单元格 contentView 并以编程方式调用您的目标控制器:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "yourCellId", for: indexPath) as! yourCell
    let gesture = UITapGestureRecognizer(target: self, action: #selector(changeVc))
            cell.contentView.addGestureRecognizer(gesture)
        return cell
    }
    

    或者您可以像这样向 imageView 单元格添加手势:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! yourCollecctionViewCell
    let gesture = UITapGestureRecognizer(target: self, action: #selector(changeVc))
        cell.yourImageView.isUserInteractionEnabled = true // don't forget to enable user interaction in your image view
        cell.yourImageView.addGestureRecognizer(gesture)
    
     return cell
    }
    

    现在编写改变VC的函数:

    @objc fileprivate func changeVc() {
        let vc = YourDestinationController()
        vc.modalPresentationStyle = .fullScreen // if you don't want full screen comment this line, 
        present(vc, animated: true, completion: nil)
    } 
    

    在导航控制器的情况下:

    @objc fileprivate func changeVc() {
        let vc = YourdestinationController()
        navigationController?.pushViewController(vc, animated: true)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      相关资源
      最近更新 更多