Junaid Ali
Junaid Ali

Reputation: 185

self.navigationController?.pushViewController not Working 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)
    }

Upvotes: 11

Views: 17914

Answers (2)

htafoya
htafoya

Reputation: 19273

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()

Upvotes: 6

Harish Singh
Harish Singh

Reputation: 765

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.

Upvotes: 13

Related Questions