I want a my app can go to a first view controller when every time users want it.
So I want to create a function to dismiss all the view controllers, regardless of whether it is pushed in navigation controllers or presented modally or opened anything methods.
I tried various ways, but I failed to dismiss all the view controllers certainly. Is there an easy way?
The best and prefered way to do this is to create an unwind segue. Just follow this documentation https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html. It can de done in code or through the interface builder.
To achieve what you want, modify your navigation stack, then do popViewController.
See this for the way to further manipulate the navigation stack. If your topmost VC is modal, dismiss it first before the code above.
Simply ask your
rootViewController
to dismiss anyViewController
if presenting.Pops all the view controllers on the stack except the root view controller and updates the display.
But if you want to go to specific controller just use the below function.
Pops view controllers until the specified view controller is at the top of the navigation stack.
Hello everyone here is the answer for Swift-4.
To go back to root view controller, you can simply call a line of code and your work will be done.
And if you have the splash screen and after that the login screen and you want to go to login screen you can simply append presentedviewcontroller in the above code.