如何从 UICollectiveViewCell 呈现视图控制器 Class - SWIFT

How to present view controller from UICollectiveViewCell Class - SWIFT

我想知道如何从 "didSelectItemAt - Method "

中呈现一个新的视图控制器

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

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) {




    }




}

首先为 viewcontroller 提供故事板 ID,然后

let destination = UIStoryboard(name: "Storyboard",bundle:nil).instantiateViewController(withIdentifier: "your_viewcontroller_id") 目前(目的地,动画:假,完成:无)

您必须为此实现自定义委托。当用户 select 项目来自集合视图时,您必须使用委托调用父视图方法。

这里是参考: