如何从 UICollectiveViewCell 类呈现视图控制器 - SWIFT [英] How to present view controller from UICollectiveViewCell Class - SWIFT

查看:23
本文介绍了如何从 UICollectiveViewCell 类呈现视图控制器 - SWIFT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从didSelectItemAt - Method"中呈现一个新的视图控制器

I want to know how I can present a new view controller from "didSelectItemAt - Method "

请注意 UIViewController 不是子类,因此我无权访问 navigationController 来呈现视图控制器.

Please note that UIViewController is not a subclass so I don't have access to the navigationController to present the view controller.

class FeedCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    let cellId = "cellId"

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.white
        cv.alwaysBounceVertical = true // Making the collection view scrollable vertically
        cv.dataSource = self
        cv.delegate = self
        return cv

    }()
    override func setupViews(){
        super.setupViews()
        addSubview(collectionView)
        addConstraintWithFormat(format: "H:|[v0]|", views: collectionView)
        addConstraintWithFormat(format: "V:|[v0]|", views: collectionView)
        collectionView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0)


    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {




    }




}

推荐答案

您必须为此实现自定义委托.当用户从集合视图中选择项目时,您必须使用委托调用父视图方法.

You have to implement custom delegate for this. When user select item from collection view, you have to call parent view method using delegate.

这里是参考:访问 UICollectionView 的父 UIViewController

这篇关于如何从 UICollectiveViewCell 类呈现视图控制器 - SWIFT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆