Bird
0
0

You want to create a button that changes its title to "Clicked" when tapped. Which code snippet correctly implements this behavior?

hard📝 Application Q15 of 15
iOS Swift - SwiftUI Basics
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?
}
Aself.button.setTitle("Clicked", for: .normal)
Bbutton.setTitle("Clicked", for: .normal)
Cbutton.title = "Clicked"
DchangeTitle.title = "Clicked"
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes