11

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)
    }
5
  • 2
    Unrelated to the issue but in this kind of scenario never retrieve data from the view (the cell), get it from the model (the data source) Commented Dec 3, 2017 at 18:45
  • Is self in a navigation controller? Commented Dec 3, 2017 at 18:45
  • Also, your if statement is always going to return true. I would suggest thoroughly reading the UICollectionView documentation before proceeding. Commented Dec 3, 2017 at 18:53
  • Also you should generally prefer the newer show(newViewController, sender: self) to directly accessing the navigation controller Commented Dec 4, 2017 at 13:27
  • problem get solved. Commented Dec 4, 2017 at 16:26

2 Answers 2

13

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.

Sign up to request clarification or add additional context in comments.

Comments

6

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.