Search code examples
iosswiftuiviewcontrolleruinavigationcontroller

How to pop back to root ViewController from viewController that was used?


I have a navigation controller that shows a view controller programmatically. Now I am trying to go back to the root view controller of the navigation controller if there is an error, but I can't seem to get back there.

The page I'm working with is presented thusly:

let vc = self.storyboard!.instantiateViewController(withIdentifier: page)
self.show(vc, sender: self)

And if I try go back duly:

self.navigationController?.popViewController(animated: true)

I get a warning rudely:

expression type UIViewController is unused

I don't want to use segues here.


Solution

  • self.To avoid warning use:

    _ = self.navigationController?.popViewController(animated: true).

    To pop back to root view controller use:-

    if let self.navigationController = self.window?.rootViewController as? UINavigationController {
         navigationController.popToRootViewControllerAnimated(true)
    }