iOS
Swift
navigationController
backButtonCallback
mobileDevelopment

back button callback in navigationController in iOS

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

When developing iOS applications using UINavigationController , managing navigation flow can often involve handling user actions on the back button. Customizing the back button and its callback mechanisms enables developers to enhance user experience by adding specific functionalities when navigating between views. Here, we explore different techniques for handling back button callbacks and offer practical insights on customizing navigation controllers.

Understanding the Back Button in UINavigationController

The back button in a UINavigationController is a key element, automatically provided after a view controller is pushed onto the navigation stack. By default, it appears in the navigation bar and allows users to move back to the previous view controller. When designing an app’s navigation flow, it’s often necessary to handle this button’s actions.

Default Behavior

The system-generated back button doesn't come with direct action handlers. However, tapping it will automatically trigger the viewWillDisappear and viewDidDisappear lifecycle methods in the view controller that is transitioning out. In the destination view controller, viewWillAppear and viewDidAppear are called.

Custom Back Button

To customize the back button, you can assign a custom view with a UIBarButtonItem . This approach not only allows you to alter the appearance but also to specify a custom callback action.

  • viewWillDisappear(_:) : Called right before the view disappears.
  • viewDidDisappear(_:) : Called after the view has disappeared.

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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms