How do I pop two views at once from a navigation controller?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UINavigationController has an API for popping one controller and an API for popping back to a known controller. It does not have a built-in method named “pop two,” but you can still achieve the effect safely by either popping to a specific earlier controller or by rewriting the navigation stack.
The right approach depends on what you know. If you know the destination controller, use popToViewController. If you only know you want to remove the top two controllers, update viewControllers directly.
Pop to a Known Controller
If the controller you want to return to is already in the stack, popToViewController(_:animated:) is the cleanest option.
If the stack is A -> B -> C -> D and D is visible, stack[stack.count - 3] is B. Popping to B removes C and D, which is exactly “pop two views at once.”
This is safer than calling popViewController twice because the user sees one coherent transition and the stack changes atomically.
Rewrite the Stack When Needed
Sometimes you do not care which controller type sits underneath; you just want to drop the top two controllers. In that case, build a shorter array and call setViewControllers.
This helper is useful for reusable flow logic, but you must ensure the resulting stack still makes sense for the user. Navigation should reflect the actual state of the workflow.
When popToRootViewController Is Better
If the real requirement is “go back to the beginning of this flow,” popping exactly two controllers may be too specific. popToRootViewController(animated:) or popToViewController targeting a named controller is often clearer and more stable.
Hard-coding “pop two” becomes fragile when the flow changes and an extra screen gets inserted later. Popping to a semantic destination usually ages better than popping by count.
For example, if an onboarding flow sometimes inserts an optional permissions screen, code based purely on stack depth can break or return to the wrong place.
Modal Versus Navigation Stack
A frequent source of confusion is mixing navigation pushes with modal presentation. If one of the visible screens was presented modally, popping the navigation controller will not dismiss it. In that case, you may need dismiss(animated:) instead of, or in addition to, a navigation pop.
Make sure you understand how the current screens were shown before changing the stack.
Common Pitfalls
A common mistake is calling popViewController(animated:) twice in a row. The first pop starts a transition immediately, and the second call may run against an already-changing stack. Use one stack operation instead.
Another issue is indexing the viewControllers array without checking its size. If the stack has fewer controllers than expected, your code will crash.
Developers also sometimes pop by count when they should pop to a known controller type. If the flow is dynamic, a semantic destination is safer than “remove exactly two.”
Finally, do not ignore state propagation. If intermediate controllers hold data that must be committed before disappearing, make sure that happens before you rewrite the stack.
Summary
- There is no dedicated “pop two” API, but the behavior is easy to implement.
- Use
popToViewControllerwhen you know the destination already exists in the stack. - Use
setViewControllerswhen you want to remove a fixed number of controllers. - Prefer semantic destinations over hard-coded stack depth when flows can change.
- Verify whether screens were pushed or presented modally before choosing the navigation API.
Related reading
- How do I prevent the iPhone screen from dimming or turning off while my application is running?
- How do I print the type or class of a variable in Swift?
- How do I programmatically restart an Android app?
- How do I put the image on the right side of the text in a UIButton?
- How do I record audio on iPhone with AVAudioRecorder?
- How do I remove lines between ListViews on Android?
- How do I remove the blue styling of telephone numbers on iPhone/iOS?
- How do I replace weak references when using ARC and targeting iOS 4.0?
.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.