How do you add an action to a button programmatically in xcode
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
To add a button action programmatically in UIKit, create the UIButton, add it to the view hierarchy, and register a target-action pair with addTarget. The most common event is .touchUpInside, which fires when the user taps and releases the button inside its bounds.
The Target-Action Pattern
UIKit buttons use the target-action mechanism. Instead of assigning an inline callback, you tell the button:
- which object should receive the event
- which method should be called
- which control event should trigger it
In Swift, that usually looks like button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside).
The selector method must be visible to the Objective-C runtime, so it needs @objc.
A Complete Example
This is the basic answer for fully programmatic UIKit views.
Selector Signatures
You can use either of these method forms:
or
The second form is often more useful because you can inspect the sender, which matters when several buttons share one action method.
Multiple Buttons, One Action
If you want several buttons to call the same method, set distinguishing properties such as tag or compare button references directly.
That keeps related user actions in one place without requiring Interface Builder outlets.
Removing or Replacing Actions
You can also remove a target-action pairing:
That is helpful when buttons change behavior based on screen state. In many cases, though, it is cleaner to keep one action and switch behavior inside the handler.
SwiftUI Is Different
If you are using SwiftUI, the pattern is different. A SwiftUI Button takes an action closure directly:
So the addTarget pattern is specifically for UIKit, not SwiftUI.
Programmatic UI Usually Pairs With Auto Layout
Developers often discover target-action at the same time they move away from storyboards. In that setup, remember that event wiring is only one part of making the button usable. You still need to add the button to the hierarchy, set translatesAutoresizingMaskIntoConstraints = false when using Auto Layout, and activate constraints so the control actually appears where users can tap it.
Common Pitfalls
The most common mistake is forgetting @objc on the selector method. Without it, the selector cannot be resolved correctly at runtime.
Another issue is using the wrong control event. For normal taps, .touchUpInside is usually correct. Developers also sometimes forget to add the button to the view hierarchy or to set up layout constraints, then assume the action registration failed when the button was never visible or tappable in the first place.
Summary
- In UIKit, register button actions with
addTarget. - Use a selector method marked with
@objc. - '
.touchUpInsideis the usual event for button taps.' - Include the sender parameter when one method handles multiple buttons.
- This target-action pattern is for UIKit; SwiftUI uses closure-based actions instead.
Related reading
- How do you add an in-app purchase to an iOS application?
- How do you add an in-app purchase to an iOS application?
- How do you add multi-line text to a UIButton?
- How do you beta test an iphone app?
- How do you change text to bold in Android?
- How do you change the launcher logo of an app in Android Studio?
- How do you check current view controller class in Swift?
- How do you check if SwiftUI is in preview mode?
.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.