Buttons let users tap to do something in your app. Action handling means making the app respond when a button is tapped.
Button and action handling in iOS Swift
Introduction
Syntax
iOS Swift
let button = UIButton(type: .system) button.setTitle("Tap me", for: .normal) button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) @objc func buttonTapped() { // code to run when button is tapped }
Use addTarget(_:action:for:) to connect the button tap to your code.
The @objc keyword is needed for the action method to work with the button.
Examples
iOS Swift
let button = UIButton(type: .system) button.setTitle("Click me", for: .normal) button.addTarget(self, action: #selector(handleClick), for: .touchUpInside) @objc func handleClick() { print("Button clicked") }
iOS Swift
let button = UIButton(type: .system) button.setTitle("Press here", for: .normal) button.addTarget(self, action: #selector(showAlert), for: .touchUpInside) @objc func showAlert() { let alert = UIAlertController(title: "Hello", message: "Button pressed!", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default)) present(alert, animated: true) }
Sample App
This app shows a button in the middle of the screen. When you tap it, an alert pops up saying "You tapped the button!".
iOS Swift
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let button = UIButton(type: .system) button.setTitle("Tap me", for: .normal) button.frame = CGRect(x: 100, y: 200, width: 120, height: 50) button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) view.addSubview(button) } @objc func buttonTapped() { let alert = UIAlertController(title: "Button Tapped", message: "You tapped the button!", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default)) present(alert, animated: true) } }
Important Notes
Always set the button frame or constraints so it appears on screen.
Use .touchUpInside event to detect taps inside the button area.
Remember to add the button to your view with addSubview.
Summary
Buttons let users interact by tapping.
Use addTarget to connect button taps to your code.
Action methods must be marked with @objc to work.
Practice
1. What is the main purpose of a UIButton in an iOS app?
easy
Solution
Step 1: Understand UIButton role
A UIButton is a UI element that users can tap to perform an action.Step 2: Compare options with UIButton purpose
Only To let users tap and trigger actions describes this interaction purpose correctly.Final Answer:
To let users tap and trigger actions -> Option AQuick Check:
UIButton = user tap action [OK]
Hint: Buttons are for user taps triggering actions [OK]
Common Mistakes:
- Confusing buttons with labels
- Thinking buttons store data
- Assuming buttons handle navigation automatically
2. Which of the following is the correct way to add an action to a UIButton in Swift?
easy
Solution
Step 1: Recall UIButton action syntax
The correct method is addTarget(_:action:for:) with #selector for the method.Step 2: Match syntax with options
Only button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) uses the correct method and parameters.Final Answer:
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) -> Option BQuick Check:
addTarget + #selector + .touchUpInside = correct [OK]
Hint: Use addTarget with #selector and .touchUpInside [OK]
Common Mistakes:
- Using nonexistent addAction method
- Assigning action directly without addTarget
- Calling the selector method instead of referencing it
3. Given the code below, what happens when the button is tapped?
let button = UIButton()
button.addTarget(self, action: #selector(showMessage), for: .touchUpInside)
@objc func showMessage() {
print("Button was tapped")
}medium
Solution
Step 1: Check action method annotation
The method showMessage is marked with @objc, so it can be called by the button.Step 2: Understand button tap behavior
When tapped, the button triggers showMessage, which prints the message.Final Answer:
The message "Button was tapped" is printed to the console -> Option CQuick Check:
@objc method called on tap = print message [OK]
Hint: @objc method runs on button tap printing message [OK]
Common Mistakes:
- Forgetting @objc on action method
- Expecting UI changes without code
- Confusing button title with action effect
4. Identify the error in this code snippet that prevents the button action from working:
let button = UIButton()
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
func buttonPressed() {
print("Pressed")
}medium
Solution
Step 1: Check method declaration for selector
Methods used with #selector must be marked @objc to be visible to Objective-C runtime.Step 2: Verify other code parts
While adding button to view and title are important, they don't prevent action from being called.Final Answer:
The action method is missing the @objc attribute -> Option DQuick Check:
@objc missing on action method = no call [OK]
Hint: Action methods need @objc to be called by button [OK]
Common Mistakes:
- Ignoring @objc requirement
- Thinking button must have title for action
- Assuming selector syntax is wrong when it's correct
5. You want to create a button that changes its title to "Clicked" when tapped. Which code snippet correctly implements this behavior?
let button = UIButton()
button.setTitle("Tap me", for: .normal)
button.addTarget(self, action: #selector(changeTitle), for: .touchUpInside)
@objc func changeTitle() {
// What goes here?
}hard
Solution
Step 1: Understand scope of button variable
The button variable is declared outside the method, so inside changeTitle() you must reference it properly, usually as self.button.Step 2: Use correct method to change title
UIButton's title is changed using setTitle(_:for:) method, not by assigning to a property.Final Answer:
self.button.setTitle("Clicked", for: .normal) -> Option AQuick Check:
Use self.button.setTitle to update title [OK]
Hint: Use self.button.setTitle("Clicked", for: .normal) inside action [OK]
Common Mistakes:
- Trying to assign title directly
- Using button without self when needed
- Calling setTitle on wrong object
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
