iOS development
navigation controller
back button
Swift programming
user interface design

Programmatically call navigation controller back button on iOS

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

On iOS, you normally do not “press the back button” programmatically. Instead, you perform the same navigation action that the back button would perform, which is usually popping the current view controller from a UINavigationController stack. The exact method depends on how the screen was shown: pushed on a navigation stack or presented modally.

Use popViewController for Standard Back Navigation

If the current screen was pushed with navigationController?.pushViewController(...), the programmatic equivalent of tapping Back is:

swift
navigationController?.popViewController(animated: true)

That removes the current view controller from the navigation stack and reveals the previous one.

A simple example:

swift
1import UIKit
2
3final class DetailViewController: UIViewController {
4    @IBAction func doneTapped(_ sender: UIButton) {
5        navigationController?.popViewController(animated: true)
6    }
7}

This is the standard answer for most cases.

Use dismiss for Modal Screens

If the screen was shown with present(...) rather than pushed onto a navigation controller, Back is not the right concept. In that case, dismiss the presented screen.

swift
dismiss(animated: true)

A common mistake is calling popViewController on a screen that is actually modal. That does nothing if there is no navigation stack to pop from.

Use popToRootViewController or popToViewController for Custom Flow

Sometimes you do not want to go back by only one screen. UINavigationController gives you other stack operations.

swift
navigationController?.popToRootViewController(animated: true)

Or to pop back to a specific screen:

swift
1if let controllers = navigationController?.viewControllers,
2   let target = controllers.first(where: { $0 is HomeViewController }) {
3    navigationController?.popToViewController(target, animated: true)
4}

These are navigation-stack operations, not simulated button taps.

Do Not Call the Back Button's Action Directly

Developers sometimes try to trigger the back button by reaching into navigationItem.leftBarButtonItem and calling its selector. That is the wrong level of abstraction. The bar button is only UI for a navigation action. The real behavior belongs to the navigation controller.

If you need custom cleanup before leaving the screen, do that work and then call the correct navigation method.

Handle Unsaved Changes Explicitly

Programmatic back navigation is often used after form submission or cancellation. If the screen contains unsaved changes, confirm with the user before popping or dismissing.

swift
1let alert = UIAlertController(title: "Discard changes?", message: nil, preferredStyle: .alert)
2alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
3alert.addAction(UIAlertAction(title: "Discard", style: .destructive) { _ in
4    self.navigationController?.popViewController(animated: true)
5})
6present(alert, animated: true)

That keeps the navigation behavior predictable and safe.

Custom Back Buttons Should Still Use the Navigation API

If you replace the default back button with your own bar button item, the handler should still call popViewController or dismiss, not duplicate stack logic manually. That preserves the navigation controller's normal behavior and keeps the code easier to reason about.

It also keeps gesture-based back navigation and stack semantics aligned with the rest of the app.

Common Pitfalls

  • Calling popViewController when the screen was presented modally.
  • Trying to invoke the back button UI instead of performing the navigation action.
  • Forgetting that navigationController can be nil if the view controller is not embedded in one.
  • Using back navigation to hide underlying state-management problems.
  • Popping multiple screens accidentally when you only wanted one level back.

Summary

  • The usual programmatic equivalent of the Back button is navigationController?.popViewController(animated: true).
  • Use dismiss(animated:) for modally presented screens.
  • Use popToRootViewController or popToViewController for custom back-navigation flows.
  • Do not try to simulate a tap on the bar button item.
  • Choose the navigation method that matches how the screen was presented.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.