Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the quotes around the title string.
Using the wrong title text.
✗ Incorrect
The button title must be set as a string with quotes, exactly "Press Me".
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist.
Missing the #selector syntax.
✗ Incorrect
The selector must match the method name exactly, which is buttonTapped.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between selector name and method name.
Missing @objc attribute.
✗ Incorrect
The method name must match the selector used when adding the target, which is buttonTapped.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .custom type without setting appearance.
Forgetting quotes around the title.
✗ Incorrect
Use .system for the button type and "Click Me" as the title string.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong button type.
Forgetting to use UIColor for colors.
Mismatch in action method name.
✗ Incorrect
Use .system for button type, UIColor.blue for title color, and buttonTapped as the action method.