How to Go Back to the Previous Screen with UINavigationController in Swift
In this article, we will explain how to navigate back to a previous screen when using a Navigation Controller in Swift.
For example, suppose you use a Navigation Controller and perform a screen transition when tapping the NEXT button, as shown below.
Here, we will show you how to return from screen No. 2 back to screen No. 1 when tapping the DONE button, or from screen No. 3 back to screen No. 1.
The app is using a Navigation Controller, and the screens are connected with a Show segue from the buttons.
How to Navigate Back to the Previous Screen in Swift
To navigate back to the previous screen using a Navigation Controller in Swift, you can use the popViewController method.
For example, if you want to return to the previous screen when the DONE button is tapped on the second screen, you can write:
import UIKit
class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func doneButtonTapped(_ sender: Any) {
self.navigationController?.popViewController(animated: true)
}
}
With self.navigationController?.popViewController(animated: true), the current top View Controller is popped from the navigation stack, and the app navigates back to the previous screen.
How to Navigate Back Multiple Screens in Swift
To go back multiple screens in a Navigation Controller flow, you can use the popToViewController method.
The first parameter of popToViewController specifies the View Controller you want to display after popping.
For example, if you want to return to the screen two steps back (No. 1) when the DONE button is tapped on the third screen, you can write:
import UIKit
class ViewController3: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func doneButtonTapped(_ sender: Any) {
if let viewControllers = self.navigationController?.viewControllers {
self.navigationController?.popToViewController(viewControllers[viewControllers.count - 3], animated: true)
}
}
}
On line 10, we retrieve the array of View Controllers currently in the navigation stack.
The last element is the current screen. To go back two screens, we access viewControllers[viewControllers.count - 3].
viewControllers[viewControllers.count - 1]: Current View Controller
viewControllers[viewControllers.count - 2]: One screen back
viewControllers[viewControllers.count - 3]: Two screens back
viewControllers[viewControllers.count - 4]: Three screens back
...
Be careful: specifying a non-existing index will cause an error.
With this, the specified View Controller is popped to, and you return to screen No. 1.
How to Navigate Back to the Root View Controller in Swift
To return to the very first screen in a Navigation Controller flow, you can use the popToRootViewController method.
For example, if you are on the third screen and want to return to the initial screen (No. 1) when tapping the DONE button, you can write:
import UIKit
class ViewController3: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func doneButtonTapped(_ sender: Any) {
self.navigationController?.popToRootViewController(animated: true)
}
}
With self.navigationController?.popToRootViewController(animated: true), all View Controllers except the root are popped from the stack, and you are navigated back to the very first screen.
That's it — we've explained how to navigate back to previous screens using a Navigation Controller in Swift.