UIBarButtonItem target-action not working?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When a UIBarButtonItem tap does nothing, the issue is almost always one of these: the target is nil (deallocated or never set), the action selector is misspelled or has the wrong signature, the button was created with a custom view that absorbs touches, or isEnabled is false. Unlike UIButton, UIBarButtonItem does not inherit from UIView and has its own quirks with the target-action pattern.
Basic Correct Setup
Key requirements:
targetisself(notnil)- Action method is marked
@objc - Selector matches the method name exactly
Problem 1: Target is nil
When target is nil, UIKit sends the action up the responder chain. If no responder implements the selector, nothing happens — no crash, no warning, just silence.
Fix: Always set target: self (or the appropriate object).
Problem 2: Missing @objc
The target-action pattern uses Objective-C message dispatch. Without @objc, the method is invisible to the runtime. In modern Swift, the compiler usually catches this with #selector, but it can slip through if you set the action as a string.
Problem 3: Wrong Selector Signature
A mismatch between the selector and method signature causes a crash (unrecognized selector) or silent failure.
Problem 4: Custom View Absorbs Touches
When you create a UIBarButtonItem with customView:, the bar button item's own target and action are not used. The custom view handles its own touches.
Fix: Add the action to the custom view directly:
Problem 5: Button is Disabled
Check that isEnabled is true. The button appears dimmed when disabled, but it can be hard to notice.
Problem 6: View Controller is Deallocated
The target is not retained by UIBarButtonItem. If the target object is deallocated, the action goes to nil (no crash, no response).
Fix: Ensure the target stays alive for the lifetime of the button.
Problem 7: System Item with Target-Action
System items (.done, .cancel, .add, etc.) work with target-action. But make sure the toolbar or navigation bar is actually visible.
Problem 8: Not in Navigation Controller
navigationItem only shows buttons when the view controller is embedded in a UINavigationController.
Debugging Checklist
Common Pitfalls
- Custom view ignores target-action:
UIBarButtonItem(customView:)does not use its owntarget/action. Add touch handling to the custom view itself. - Weak reference to target:
UIBarButtonItemdoes not retain its target. If the target is deallocated (common with closures or temporary objects), taps silently fail. - Setting action after init: Assigning
barButton.action = #selector(newAction)works, but make surebarButton.targetis also set — it defaults tonil. - Toolbar vs Navigation bar:
toolbarItemsappear in the toolbar (at the bottom),navigationItembuttons appear in the navigation bar (at the top). Putting buttons in the wrong collection makes them invisible. - Responder chain confusion: With
target: nil, the action travels the responder chain. This works only if a responder in the chain implements the selector. It is fragile and hard to debug — always set an explicit target.
Summary
- Always set
target: selfand mark action methods with@objc - Ensure the selector matches the method signature exactly (
saveTappedvssaveTapped(_:)) - Custom view bar button items ignore
target/action— add actions to the custom view directly - Check
isEnabled, verify the target is not deallocated, and confirm aUINavigationControllerexists - Use
responds(to:)and print debugging to verify the target-action chain
Related reading
- UIBarButtonItem with custom image and no border
- UIButton custom font vertical alignment
- UIButton doesn't listen to content mode setting?
- UIButton events. What's the difference?
- UICollectionReusableView method not being called
- UICollectionView reloadData not functioning properly in iOS 7
- UIButton how to center an image and a text using imageEdgeInsets and titleEdgeInsets?
- UIButton Image Text IOS
.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.