【问题标题】:How to present view controller from UICollectiveViewCell Class - SWIFT如何从 UICollectiveViewCell 类呈现视图控制器 - SWIFT
【发布时间】:2018-02-01 02:29:04
【问题描述】:

我想知道如何从“didSelectItemAt - 方法”中呈现一个新的视图控制器

请注意 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) {




    }




}

【问题讨论】:

    标签: swift3 uinavigationcontroller viewcontroller


    【解决方案1】:

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

    这是参考:Access a UICollectionView's parent UIViewController

    【讨论】:

    • 谢谢。即使花了一些时间才弄清楚,这种方法也很有效。
    【解决方案2】:

    首先给viewcontroller一个storyboard id,然后

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

    【讨论】:

    • 我没有使用情节提要来构建应用程序。一切都是在没有故事板的情况下完成的,所以这种方法仍然可行吗?
    • 这将导致应用程序终止并出现以下异常:“应用程序试图在其自身上呈现模态视图控制器。”
    猜你喜欢
    • 1970-01-01
    • 2015-06-17
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多