【问题标题】:show viewController from collectionView without storyboards从没有情节提要的 collectionView 中显示 viewController
【发布时间】:2017-06-24 16:06:36
【问题描述】:

我在从 collectionView 中以编程方式(没有情节提要)显示 viewController 时遇到了一些问题。我认为这很容易,但我很难弄清楚这一点,因为我对 swift 有点陌生。我相信 collectionViewCells 默认不是委托,所以我尝试在 collectionView 类中实现 didSelectItemAt 但仍然没有运气。在单元格的水龙头上,我收到了一份打印声明,只是没有任何表演节目。请参阅下面的 collectionViewCell 代码,提前致谢!

// collectionViewCell class
class PlanningCell: BaseCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    //collectionViewCell variables
    var plannedPlaces = [CurrentPlanner]()   
    let cellId = "cellId"

    var basePlanningCell: BasePlanningCell!

    override func setupViews() {
        super.setupViews()
        addSubview(collectionView)

        collectionView.register(BasePlanningCell.self, forCellWithReuseIdentifier: cellId)
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! BasePlanningCell
        cell.currentPlanner = plannedPlaces[indexPath.item]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("did tap")
        let plannersDetailVC = PlanningPlaceDetailsVC()
        plannersDetailVC.navigationTitle = plannedPlaces[(indexPath.row)].planningPlace
        // below I am receiving the use of unresolved identifier "show" error
        show(plannersDetailVC, sender: self)
    }

}

【问题讨论】:

    标签: swift delegates uicollectionview segue uicollectionviewcell


    【解决方案1】:

    一般来说,当你想呈现另一个视图控制器时,你需要调用这个函数

    self.present(yourViewController, animated: true, completion: nil)
    

    但是,您不能在打印的地方执行此操作,因为函数 present 仅在视图控制器中可用。所以现在问题变成了,如何将您的单元格单击指向父视图控制器并传递一个字符串。你有两个选择,你可以创建一个像this 这样的委托,或者创建一个NSNotificationCenter post call,第二个更容易。

    当你将单元格点击事件传递给你的父视图控制器时,你可以开始调用上面提到的方法来导航到另一个视图控制器

    【讨论】:

    • 谢谢,但它现在给了我错误消息“'PlanningCell' 类型的值没有成员'存在'”
    • @user3708224 更新了我的答案
    【解决方案2】:
    let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "VCIdentifier") as! PlanningPlaceDetailsVC
            present(vc, animated: true, completion: nil)
    

    别忘了设置 PlanningPlaceDetailsVC 标识符

    为代码设置的 VC 更新:

    let vc : UIViewController = PlanningPlaceDetailsVC()
    self.presentViewController(vc, animated: true, completion: nil)
    

    【讨论】:

    • 我根本没有使用故事板。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2011-12-26
    相关资源
    最近更新 更多