1

I am having some trouble showing a viewController programmatically (without storyboards) from a collectionView. I would think this is pretty easy but I am having some difficulty figuring this out as I am somewhat new to swift. I believe collectionViewCells are not delegates by default, so I have tried implementing the didSelectItemAt in the collectionView class but still no luck. On tap of the cell I am receiving a print statement, just not having any show segue. See collectionViewCell code below, and thanks in advance!

// 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)
    }

}
user3708224
  • 1,229
  • 4
  • 19
  • 37

2 Answers2

0
let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "VCIdentifier") as! PlanningPlaceDetailsVC
        present(vc, animated: true, completion: nil)

Don't forget to set the PlanningPlaceDetailsVC Identifier

Updated for a VC setted by code:

let vc : UIViewController = PlanningPlaceDetailsVC()
self.presentViewController(vc, animated: true, completion: nil)
Hassan Taleb
  • 2,350
  • 2
  • 19
  • 24
0

Normally, when you want to present another view controoler, you need to call this function

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

However, you cannot do that in the place you do the print because the function present is only available in view controllers. So now the problem becomes, how to direct your cell click to the parent view controller and pass a string. You have two options, you can either create a delegate like this, or do a NSNotificationCenter post call, the second one being easier.

When you pass the cell click event to your parent view controller, you can start calling that method mention above to navigate to another view controoler

Fangming
  • 24,551
  • 6
  • 100
  • 90