Search code examples
iosswift4xcode10

CollectionView navigationController?.pushViewController() does nothing with no error


I have created a CollectionView and I want to open a ViewController with specific details depending on which row was clicked.(I'm not using a storyboard this is all done programatically).

enter image description here

I've looked at the documentation and I am trying to use the override function didSelectItemAt and navigationController?.pushViewController() but nothing seems to happen. No error or anything? See my Code:

(CollectionViewController) enter image description here

(ViewController) enter image description here

I've also tried supplying the layout to the viewcontroller but no luck. It is clearly accepting the click function of each row because it prints out to the log fine:

enter image description here

Any help to what i've missed here would be appreciated.


Solution

  • First, make sure in your TabBarController, the controller is initialized as navigationController

    func viewDidLoad() {
        let vc1 = UINavigationController(rootViewController: YourController())
        viewControllers = [vc1, vc2]
    }
    

    before you put it into viewControllers array, so that you could call navigationController?.pushViewController() in vc1

    Another thing: Make sure you give a flow layout when initializing CollectionViewController before push, like so:

    let layout = UICollectionViewFlowLayout()
    let detailVC = PlaceDetailController(collectionViewLayout: layout)
    detailVC.place = ... // Pass value here
    navigationController?.pushViewController(detailVC, animated: true)