Can't dismiss View Controller that's embed

2019-07-27 17:30发布

问题:

I have the following scenario. A view controller that's embedded in a navigation controller.

This view controller has a button that instantiate a tab bar controller, then presents one of its view controllers.

When i dismiss the controller presented by the tab bar controller, i always end up in the initial view controller, in the same instance of it.

What i tried is:

    func showHomeScreen()  {
    //trying to dismiss the current view controller ( will move this code out of this method once i figure out how to do it
    self.navigationController?.popViewController(animated: true)
    self.dismiss(animated: true, completion: nil)
    //showing the next view controller
    let tabBarController = storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController

    tabBarController.selectedViewController = tabBarController.viewControllers?[1]
    present(tabBarController, animated: true, completion: nil)
}

self.navigationController?.popViewController => returns nil self.dismiss(animated: true, completion: nil) doesn't seem to do anything.

Can anyone please tell me how to fix this, i imagine its something terribly simple but i can't find the answer to it.

Edit: This is how my storyboard looks:

https://pasteboard.co/HVdHp6P.png

https://pasteboard.co/HVdHHoG.png

回答1:

After sending to the next viewController, try using:

navigationController?.viewControllers.remove(at: 1)

This should remove the viewController that is second in the stack.



回答2:

In case of view controller that has been pushed by navigation controller, this works pretty well:

self.navigationController?.popToRootViewController(animated: true)