0

I thought that I had this code correct; however, when I run it, I click on the cell and nothing happens. The code that I have to push the view controller is in my didSelectRowAt func.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let vc = ChatViewController()
        vc.title = "Jenny Smith"
        vc.navigationItem.largeTitleDisplayMode = .never
        navigationController?.pushViewController(vc, animated: true)
}

Can anyone explain what I'm doing wrong here and why a click doesn't show a new ViewController?

  • What you have looks correct to me. Is the `navigationController` nil? If you set a breakpoint in this code, does the debugger stop in this code when you select a table view item? – Scott Thompson Oct 08 '21 at 23:38
  • Are you sure you have a nav controller? Also set a background colour for vc.view, just to make sure you aren't loading a clear view and not seeing it. – flanker Oct 08 '21 at 23:42
  • tableview.delegate = self have you did this? – Yogesh Patel Oct 09 '21 at 08:21
  • @flanker a nav controller for for the vc that I'm trying to switch to or a nav controller for the vc that I'm writing code to? – bigyoshi Oct 18 '21 at 21:55
  • Would be the same for both - it manages the VCs pushed to / popped from its stack. As a quick and dirty test, change `navigationController?.pushViewController` to `navigationController!.pushViewController` as then if it's nil it will crash. (this is jus for debugging, not for production!) – flanker Oct 19 '21 at 08:31

1 Answers1

0

You must try this to solve the problem, if there is any issues else you can ask me

let VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChatViewController") as! ChatViewController
self.navigationController?.pushViewController(VC, animated: false)
Rahul John
  • 468
  • 1
  • 3
  • 6