Sobes.tech
Junior — Middle

How to choose the appropriate mechanism for updating the label text when clicking the button?

sobes.tech AI

Answer from AI

To update the text of a UILabel when clicking a button in iOS, the usual mechanism is to connect the button's action to a function that changes the label's text property. The simplest way is to use IBAction in the controller and update the label's text property inside it.

Example in Swift:

@IBOutlet weak var label: UILabel!

@IBAction func buttonTapped(_ sender: UIButton) {
    label.text = "New text"
}

For more complex applications, you can consider using MVVM patterns with binding via Combine or RxSwift, so that UI updates reactively when data changes. But for simple cases, IBAction and direct modification of text property suffice.