0
0
iOS Swiftmobile~10 mins

Button and action handling in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a button with the title "Press Me".

iOS Swift
let button = UIButton()
button.setTitle([1], for: .normal)
Drag options to blanks, or click blank then click option'
A"Button"
B"Click"
C"Tap"
D"Press Me"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the quotes around the title string.
Using the wrong title text.
2fill in blank
medium

Complete the code to add an action to the button that calls the method buttonTapped when pressed.

iOS Swift
button.addTarget(self, action: #selector([1]), for: .touchUpInside)
Drag options to blanks, or click blank then click option'
AbuttonTapped
BtapButton
CbuttonPressed
DpressedButton
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist.
Missing the #selector syntax.
3fill in blank
hard

Fix the error in the method signature for the button action handler.

iOS Swift
@objc func [1](sender: UIButton) {
    print("Button was tapped")
}
Drag options to blanks, or click blank then click option'
AbuttonTap
BbuttonTapped
CbuttonTappedAction
DtapButton
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between selector name and method name.
Missing @objc attribute.
4fill in blank
hard

Fill both blanks to create a button, set its title, and add it to the view.

iOS Swift
let button = UIButton(type: [1])
button.setTitle([2], for: .normal)
view.addSubview(button)
Drag options to blanks, or click blank then click option'
A.system
B"Click Me"
C.custom
D"Press"
Attempts:
3 left
💡 Hint
Common Mistakes
Using .custom type without setting appearance.
Forgetting quotes around the title.
5fill in blank
hard

Fill all three blanks to create a button, set its title color, and add an action.

iOS Swift
let button = UIButton(type: [1])
button.setTitleColor([2], for: .normal)
button.addTarget(self, action: #selector([3]), for: .touchUpInside)
Drag options to blanks, or click blank then click option'
A.system
BUIColor.blue
CbuttonTapped
D.custom
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong button type.
Forgetting to use UIColor for colors.
Mismatch in action method name.