user979331
user979331

Reputation: 11961

popToRootViewController then pushViewController

I am trying to write a piece of code that will pop to the root view controller, then will push another view controller, I have this so far:

self.navigationController?.popToRootViewController(animated: true)

let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Posts") as! PostsController

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

The navigation controller is going to the root view controller, but its not pushing the other view controller, I get this is my console log:

Presenting view controllers on detached view controllers is discouraged

Is what I am trying to do not possible?

Upvotes: 0

Views: 738

Answers (1)

Aakash
Aakash

Reputation: 2269

Try this -

if let navCont = navigationController {
    var controllers = navCont.viewControllers
    controllers.removeLast()
    let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Posts") as! PostsController
    controllers.append(viewController)
    navCont.setViewControllers(controllers, animated: true)
}

Upvotes: 1

Related Questions