Bird
0
0

How can you update a UIButton's title to "Pressed" immediately when it is tapped, using Swift code inside the action handler?

hard📝 Application Q9 of 15
iOS Swift - SwiftUI Basics
How can you update a UIButton's title to "Pressed" immediately when it is tapped, using Swift code inside the action handler?
Abutton.setTitle("Pressed", for: .normal)
Bbutton.titleLabel?.text = "Pressed"
Cbutton.setTitle("Pressed", for: .highlighted)
Dbutton.currentTitle = "Pressed"
Step-by-Step Solution
Solution:
  1. Step 1: Use setTitle(_:for:) method

    This method updates the button's title for a specific state.
  2. Step 2: Use .normal state to change the default title

    Setting the title for .normal updates what is shown when button is not highlighted.
  3. Final Answer:

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

    Correct method and state used to update title immediately [OK]
Quick Trick: Use setTitle(_:for: .normal) to update button title [OK]
Common Mistakes:
  • Changing titleLabel?.text directly (won't update UI properly)
  • Setting title for .highlighted state only
  • Trying to assign to currentTitle property (read-only)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes