Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

self.navigationController?.pushViewController not Working Swift

Tags:

ios

swift

I have CollectionViewController, when I am trying to click on cell and navigate to respective ViewControllers its not working.how can I solve this issue.

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell:AddOptionCollectionViewCell = collectionView.cellForItem(at: indexPath) as! AddOptionCollectionViewCell

    if (cell.name.text == "CONTRAST"){
        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let newViewController = storyBoard.instantiateViewController(withIdentifier: "ContrastViewController") as! ContrastViewController
        self.navigationController?.pushViewController(newViewController, animated: true)
    }
like image 403
Junaid Ali Avatar asked Dec 03 '17 18:12

Junaid Ali


2 Answers

I think this problem is due to nil value of navigation stack for your CollectionViewController class. So, first of all go to storyboard and select CollectionViewController class and embed NavigationController into it. After this try and run it will work.

All the best.

like image 141
Harish Singh Avatar answered Oct 18 '22 15:10

Harish Singh


In my case, I mistakenly added the MainViewController to the window.rootViewController instead of adding it to the UINavigationController and then using the navigation as rootViewController.

at SceneDelegate.swift, it should be:

 let navigationController = UINavigationController()
 navigationController.pushViewController(MainRouter.createModule(using: navigationController), animated: false)
 window.rootViewController = navigationController
 window.makeKeyAndVisible()
like image 3
htafoya Avatar answered Oct 18 '22 13:10

htafoya