0

I am trying to navigate from ConversationsListVC (source) to ConversationVC (destination) programmatically by instantiating the source VC (self) as the root navigation controller VC and then instantiating the destination VC so that I can push navigate.

I get the print statements onto the console (see below) but the app does not navigate to the destination VC, the app doesn't crash or throw any error. what am I missing here?

debug 1
debug 2

function:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    print("debug 1")

    var window: UIWindow!

    guard let convoVC = storyboard?.instantiateViewController(withIdentifier: "ConversationVC") as? ConversationVC else { return }

    window?.rootViewController = UINavigationController(rootViewController: self)

    navigationController?.pushViewController(convoVC, animated: true)

    print("debug 2")


}//end func
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Roggie
  • 1,157
  • 3
  • 16
  • 40

2 Answers2

0

try this one maybe the window instance is not correctly.

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let newViewController = storyBoard.instantiateViewController(withIdentifier: "ConversationVC") as! ConversationVC
        navigationController?.pushViewController(newViewController, animated: true)

and make sure your viewcontroller is assign with the Identifier

Osman
  • 1,496
  • 18
  • 22
0

You can't just randomly create a new UINavigationController and push the destination onto it. You need to push the destination VC onto the UINavigationController in which self is currently embedded in.

If self is embedded in a UINavigationController already, do:

self.navigationController?.pushViewController(convoVC, animated: true)

If the source VC is not embedded in a UINavigationController, you need to do that in the storyboard.

Since you are using storyboards, you can also consider adding a push segue between the source and destination, and performing that segue:

self.performSegue(withIdentifier: "someIdentifier", sender: self)
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • thanks @Sweeper, the source VC is currently not embedded in a navigation controller and i'm reluctant to as I have built UI elements which are embedded that make-up the navigation controls at the top of the VC. I have added a segue between the VCs from the storyboard but I would like to push the destination controller from right-left, is there a way of doing that? – Roggie Jul 30 '18 at 03:56
  • @Roggie Sounds like you should write your own subclass of `UINavigationController` and `UINavigationBar`. Another alternative is to write your own custom segue. – Sweeper Jul 30 '18 at 03:58
  • funny you say that, check this earlier [question] (https://stackoverflow.com/questions/51585623/why-does-the-messagekit-jsqmessagesviewcontroller-replacement-input-field-di) I posted. The custom segue-from-right works perfectly but the default message input field at the bottom disappears. – Roggie Jul 30 '18 at 04:02