0
0
iOS Swiftmobile~20 mins

Button and action handling in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Button Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)")
    }
  }
}
AThe count number increases by 1 each time the button is tapped and updates the text.
BThe button does nothing because the count variable is not updated correctly.
CThe app crashes because the count variable is not declared properly.
DThe text shows the initial count but never updates when the button is tapped.
Attempts:
2 left
💡 Hint
Look at how @State is used to update the count variable and how the button action modifies it.
📝 Syntax
intermediate
2: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 here
Abutton.addTarget(self, action: buttonTapped(), for: .touchUpInside)
Bbutton.addAction(#selector(buttonTapped), for: .touchUpInside)
Cbutton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
Dbutton.addTarget(self, selector: buttonTapped, for: .touchUpInside)
Attempts:
2 left
💡 Hint
Remember the method signature for adding a target-action in UIKit.
lifecycle
advanced
2: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)
}
AAdding the target-action outside viewDidLoad may cause the button to not respond because the button might not be initialized yet.
BAdding the target-action outside viewDidLoad causes a compile-time error.
CAdding the target-action outside viewDidLoad works the same as inside viewDidLoad with no difference.
DAdding the target-action outside viewDidLoad causes the app to crash at runtime.
Attempts:
2 left
💡 Hint
Think about when the button is created and when viewDidLoad runs.
🔧 Debug
advanced
2: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)")
    }
  }
}
AThe Text view is missing from the body, so it cannot update.
BThe button action syntax is incorrect and does not run.
CThe count variable is a constant and cannot be changed.
DThe count variable is not marked with @State, so changes do not trigger view updates.
Attempts:
2 left
💡 Hint
Check how SwiftUI tracks changes to variables to update the UI.
🧠 Conceptual
expert
3: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.
AUIButton and SwiftUI Button both use closures for actions but SwiftUI requires @State variables.
BUIButton uses target-action pattern requiring selectors and runtime message sending; SwiftUI Button uses closures capturing state directly for declarative UI updates.
CUIButton actions are synchronous while SwiftUI Button actions are always asynchronous.
DUIButton actions require Interface Builder connections; SwiftUI Button actions require delegate methods.
Attempts:
2 left
💡 Hint
Think about how UIKit and SwiftUI handle UI updates and user interactions differently.