Programmatically go back to previous ViewController in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Going back to a previous screen in UIKit depends on how the current view controller was shown. If it was pushed onto a UINavigationController, you usually pop it. If it was presented modally, you dismiss it. The most important part is understanding your navigation style before writing the code.
Go Back in a Navigation Controller
If the current view controller was pushed onto a navigation stack, use popViewController.
This removes the current view controller from the stack and reveals the previous one.
That is the standard answer for push-based navigation such as:
- tapping from a list into a detail page
- using a
showsegue inside a navigation controller - programmatically pushing with
pushViewController
Dismiss a Modally Presented View Controller
If the screen was presented with present, do not use popViewController. Dismiss it instead.
This is the right choice for sheets, full-screen modals, and other presentation styles that are not part of a navigation stack.
Pop to a Specific Earlier Screen
Sometimes "go back" does not mean only one level. You may want to jump back to a known controller already in the stack.
This is useful in flows such as checkout or onboarding when you want to skip several intermediate screens.
If you want to return all the way to the root:
Decide Based on Presentation Style
A simple rule helps:
- pushed with navigation controller: pop
- presented modally: dismiss
If you are not sure which one applies, inspect how the view controller was created. The same screen can behave differently depending on how it entered the hierarchy.
You can even write fallback logic:
That is convenient for reusable screens that may appear in more than one navigation context.
Storyboards and Buttons
If the action is wired from Interface Builder, the code is the same. The only difference is where the @IBAction comes from.
You can connect this to a custom button or bar button item in the storyboard.
If the navigation bar already shows the default back button, you often do not need extra code at all. Programmatic back navigation is mostly for custom flows or custom controls.
Common Pitfalls
The most common mistake is calling popViewController on a modally presented screen. Nothing happens because there is no navigation stack to pop from.
Another issue is assuming navigationController is always present. It is optional, and it will be nil when the controller is not embedded in a navigation controller.
Developers also sometimes bypass the expected back behavior by creating a brand new previous screen instead of returning to the existing one. That usually breaks navigation state and duplicates controllers unnecessarily.
Finally, be careful when jumping to a specific controller in the stack. If it is not there, popToViewController cannot do anything.
Summary
- Use
navigationController?.popViewController(animated:)for push-based navigation. - Use
dismiss(animated:)for modally presented screens. - Use
popToViewControllerorpopToRootViewControllerwhen you need to jump back more than one level. - Check whether the controller is embedded in a navigation controller before popping.
- Choose the back-navigation API based on how the screen was originally presented.
Related reading
- Programmatically go back to the previous fragment in the backstack
- Programmatically navigate to another view controller/scene
- Programmatically obtain the phone number of the Android phone
- Programmatically open Maps app in iOS 6
- Programmatically retrieve memory usage on iPhone
- Programmatically scroll a UIScrollView
- Programmatically Select all text in UITextField
- Programmatically selecting text in an input field on iOS devices mobile Safari
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.