Challenge - 5 Problems
Button Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when this SwiftUI button is tapped?
Consider this SwiftUI code snippet. What will the button do when tapped?
iOS Swift
struct ContentView: View {
@State private var count = 0
var body: some View {
VStack {
Button("Tap me") {
count += 1
}
Text("Count: \(count)")
}
}
}Attempts:
2 left
💡 Hint
Look at how @State is used to update the count variable and how the button action modifies it.
✗ Incorrect
The @State property wrapper allows SwiftUI to track changes to the count variable. When the button is tapped, count increases by 1, and the Text view updates automatically to show the new count.
📝 Syntax
intermediate2:00remaining
Which option correctly connects a UIButton to an action in UIKit?
You want to add an action to a UIButton in UIKit programmatically. Which code snippet correctly adds the action?
iOS Swift
let button = UIButton(type: .system)
// Add target-action hereAttempts:
2 left
💡 Hint
Remember the method signature for adding a target-action in UIKit.
✗ Incorrect
The correct method is addTarget(_:action:for:). The action parameter requires a selector using #selector syntax. Calling the method (buttonTapped()) or using wrong parameter names causes errors.
❓ lifecycle
advanced2:00remaining
What is the effect of adding a button action inside viewDidLoad in a UIViewController?
In UIKit, you add a button and connect its action inside viewDidLoad. What happens if you add the target-action outside viewDidLoad instead?
iOS Swift
override func viewDidLoad() {
super.viewDidLoad()
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
}Attempts:
2 left
💡 Hint
Think about when the button is created and when viewDidLoad runs.
✗ Incorrect
viewDidLoad is called after the view controller's view is loaded into memory, so the button is guaranteed to exist. Adding target-action before that may fail if the button is nil or not yet created.
🔧 Debug
advanced2:00remaining
Why does this SwiftUI button not update the text when tapped?
Look at this SwiftUI code. The button is tapped but the text does not change. What is the problem?
iOS Swift
struct ContentView: View {
var count = 0
var body: some View {
VStack {
Button("Tap") {
count += 1
}
Text("Count: \(count)")
}
}
}Attempts:
2 left
💡 Hint
Check how SwiftUI tracks changes to variables to update the UI.
✗ Incorrect
In SwiftUI, only variables marked with @State or other property wrappers trigger UI updates when changed. Here, count is a plain variable, so changing it does not refresh the view.
🧠 Conceptual
expert3:00remaining
Which statement best describes the difference between UIButton target-action and SwiftUI Button action?
Choose the best explanation of how button actions differ between UIKit's UIButton and SwiftUI's Button.
Attempts:
2 left
💡 Hint
Think about how UIKit and SwiftUI handle UI updates and user interactions differently.
✗ Incorrect
UIKit's UIButton uses the target-action pattern with selectors, which is an older, imperative style. SwiftUI's Button uses closures that capture state variables, enabling a declarative UI that updates automatically when state changes.