Setting action for back button in navigation controller
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In a UINavigationController, the default back button automatically pops the current view controller. That is convenient, but it also means there is no simple "set custom callback on the built-in back button" API. If you need custom behavior, the usual solution is to replace the default back button or detect that the controller is being popped.
Understand the Default Behavior First
When controller B is pushed from controller A, the back button shown on B is generated from navigation state. It is not just a normal button that you own directly.
That is why this requirement usually falls into one of two categories:
- run custom code before navigating back
- detect that a back navigation already happened
Those two cases should be handled differently.
Option 1: Replace the Back Button
If you want full control over the action, hide the default back button and add your own left bar button item:
This is the right approach if you need to save draft changes, confirm navigation, or log an analytics event before popping.
Option 2: Detect a Pop in Lifecycle Code
If you only want to know that the user went back, but you do not need to block it, check whether the controller is being removed from its parent:
This is useful for cleanup, lightweight analytics, or releasing temporary resources.
It is not a replacement for a confirmation dialog because by the time this runs, the navigation is already in progress.
Handling Unsaved Changes
Unsaved changes are the most common reason people want a custom back action. In that case, replace the button and show an alert:
That pattern is more reliable than trying to intercept the internal back button after the fact.
Gesture Navigation Matters Too
When you replace the default back button, be aware of the interactive swipe-back gesture. Depending on configuration, a custom left button may affect the standard navigation experience. Test both tapping and edge-swipe navigation so the screen does not behave inconsistently.
If your screen must block leaving because of unsaved work, handle the gesture case as part of the overall navigation policy rather than assuming only button taps matter.
When You Only Want to Change the Back Title
Sometimes the real need is not a custom action but a custom title. That is set on the previous controller, not the current one:
That preserves native back behavior while changing the label.
Common Pitfalls
The biggest mistake is trying to attach a target-action handler directly to the default back button. The navigation controller manages it; you do not.
Another mistake is using viewWillDisappear to show a confirmation dialog. That is too late for true interception and can produce awkward navigation behavior.
A third issue is replacing the back button without retesting swipe gestures or accessibility behavior.
Summary
- The built-in navigation back button is managed by
UINavigationController, not by your view controller directly. - To run custom logic before going back, hide the default back button and add a custom left bar button item.
- To detect that a pop happened, check
isMovingFromParentinviewWillDisappear. - Use a confirmation alert for unsaved changes instead of trying to hijack the default button.
- If you only need a different label, change the back button title rather than replacing the whole behavior.
Related reading
- Setting alpha on UIView sets the alpha on its subviews which should not happen
- Setting an image for a UIButton in code
- Setting background colour of Android layout element
- Setting Corner Radius on UIImageView not working
- Setting custom UITableViewCells height
- Setting direction for UISwipeGestureRecognizer
- Setting text in EditText Kotlin
- Setting the zoom level for a MKMapView
.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.