Bird
Raised Fist0
iOS Swiftmobile~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand UIButton role

    A UIButton is a UI element that users can tap to perform an action.
  2. Step 2: Compare options with UIButton purpose

    Only To let users tap and trigger actions describes this interaction purpose correctly.
  3. Final Answer:

    To let users tap and trigger actions -> Option A
  4. 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

  1. Step 1: Recall UIButton action syntax

    The correct method is addTarget(_:action:for:) with #selector for the method.
  2. Step 2: Match syntax with options

    Only button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) uses the correct method and parameters.
  3. Final Answer:

    button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) -> Option B
  4. Quick Check:

    addTarget + #selector + .touchUpInside = correct [OK]
Hint: Use addTarget with #selector and .touchUpInside [OK]
Common Mistakes:
  • Using nonexistent addAction method
  • Assigning action directly without addTarget
  • Calling the selector method instead of referencing it
3. Given the code below, what happens when the button is tapped?
let button = UIButton()
button.addTarget(self, action: #selector(showMessage), for: .touchUpInside)

@objc func showMessage() {
    print("Button was tapped")
}
medium
A. The app crashes due to missing button title
B. Nothing happens because the method is missing @objc
C. The message "Button was tapped" is printed to the console
D. The button changes color automatically

Solution

  1. Step 1: Check action method annotation

    The method showMessage is marked with @objc, so it can be called by the button.
  2. Step 2: Understand button tap behavior

    When tapped, the button triggers showMessage, which prints the message.
  3. Final Answer:

    The message "Button was tapped" is printed to the console -> Option C
  4. Quick Check:

    @objc method called on tap = print message [OK]
Hint: @objc method runs on button tap printing message [OK]
Common Mistakes:
  • Forgetting @objc on action method
  • Expecting UI changes without code
  • Confusing button title with action effect
4. Identify the error in this code snippet that prevents the button action from working:
let button = UIButton()
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)

func buttonPressed() {
    print("Pressed")
}
medium
A. The button title is not set
B. The button is not added to the view
C. The selector syntax is incorrect
D. The action method is missing the @objc attribute

Solution

  1. Step 1: Check method declaration for selector

    Methods used with #selector must be marked @objc to be visible to Objective-C runtime.
  2. Step 2: Verify other code parts

    While adding button to view and title are important, they don't prevent action from being called.
  3. Final Answer:

    The action method is missing the @objc attribute -> Option D
  4. Quick Check:

    @objc missing on action method = no call [OK]
Hint: Action methods need @objc to be called by button [OK]
Common Mistakes:
  • Ignoring @objc requirement
  • Thinking button must have title for action
  • Assuming selector syntax is wrong when it's correct
5. You want to create a button that changes its title to "Clicked" when tapped. Which code snippet correctly implements this behavior?
let button = UIButton()
button.setTitle("Tap me", for: .normal)
button.addTarget(self, action: #selector(changeTitle), for: .touchUpInside)

@objc func changeTitle() {
    // What goes here?
}
hard
A. self.button.setTitle("Clicked", for: .normal)
B. button.setTitle("Clicked", for: .normal)
C. button.title = "Clicked"
D. changeTitle.title = "Clicked"

Solution

  1. Step 1: Understand scope of button variable

    The button variable is declared outside the method, so inside changeTitle() you must reference it properly, usually as self.button.
  2. Step 2: Use correct method to change title

    UIButton's title is changed using setTitle(_:for:) method, not by assigning to a property.
  3. Final Answer:

    self.button.setTitle("Clicked", for: .normal) -> Option A
  4. Quick Check:

    Use self.button.setTitle to update title [OK]
Hint: Use self.button.setTitle("Clicked", for: .normal) inside action [OK]
Common Mistakes:
  • Trying to assign title directly
  • Using button without self when needed
  • Calling setTitle on wrong object
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard