What is the purpose of a UIButton in iOS development?
A UIButton is a user interface element that users can tap to trigger an action or event in the app.
Click to reveal answer
beginner
How do you connect a UIButton to an action in Swift code?
You connect a UIButton to an action by creating an @IBAction function and linking it to the button's Touch Up Inside event in Interface Builder or programmatically.
Click to reveal answer
beginner
What does the @IBAction keyword indicate in Swift?
@IBAction marks a method that can be connected to a user interface element event, like a button tap, allowing the method to be called when the event occurs.
Click to reveal answer
intermediate
How can you add a button action programmatically in Swift?
You create a UIButton instance, then use addTarget(_:action:for:) to specify the method to call when the button is tapped.
Click to reveal answer
intermediate
Why is it important to specify the event type like .touchUpInside when adding a button action?
Specifying the event type ensures the action triggers only on the intended user interaction, such as when the user lifts their finger inside the button area, preventing accidental triggers.
Click to reveal answer
Which keyword is used to mark a method as an action for a UIButton in Swift?
A@IBOutlet
Bvar
Cfunc
D@IBAction
✗ Incorrect
@IBAction marks a method that can be connected to UI events like button taps.
What event type is commonly used to trigger a button action when the user taps it?
A.valueChanged
B.touchDown
C.touchUpInside
D.editingDidEnd
✗ Incorrect
.touchUpInside triggers the action when the user lifts their finger inside the button.
How do you connect a UIButton to an action in Interface Builder?
ADrag from the button to the @IBAction method
BDrag from the button to the @IBOutlet variable
CWrite code in viewDidLoad
DUse addSubview
✗ Incorrect
You drag from the button to the @IBAction method to connect the action.
Which method adds an action to a UIButton programmatically?
AaddTarget(_:action:for:)
BaddAction(_:for:)
CaddGestureRecognizer(_:)
DaddSubview(_:)
✗ Incorrect
addTarget(_:action:for:) is used to add an action to a UIButton programmatically.
What happens if you do not specify the event type when adding a button action?
AThe action will never be called
BThe action triggers on all events
CThe action triggers only on .touchDown
DThe app crashes
✗ Incorrect
If no event is specified, the action may trigger on multiple events, causing unexpected behavior.
Explain how to create a button and handle its tap action in Swift using Interface Builder.
Think about the steps from dragging a button to writing the action method.
You got /4 concepts.
Describe how to add a button and its action programmatically in Swift code.
Focus on creating the button and linking the tap event in code.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a UIButton in an iOS app?
easy
A. To let users tap and trigger actions
B. To display static text only
C. To store data persistently
D. To manage app navigation automatically
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 A
Quick 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
A. button.onTap = buttonTapped()
B. button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
C. button.addAction(#selector(buttonTapped))
D. button.action = #selector(buttonTapped)
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 B