How to programmatically change UIButton text in Swift? (iOS)
Hi everyone! I'm new to iOS development in Swift and ran into a problem: I can't figure out how to properly change a button's text programmatically after it has been created in the storyboard.
I have a simple button connected via IBOutlet:
```swift
@IBOutlet weak var myButton: UIButton!
```
I want the button's text to change from "Press me" to "Pressed!" when it's tapped. I tried writing this code in the action (IBAction):
```swift
@IBAction func buttonTapped(_ sender: UIButton) {
myButton.text = "Pressed!"
}
```
But Xcode gives an error: "Value of type 'UIButton' has no member 'text'". I also tried `myButton.title = "Pressed!"` — that doesn't work either.
Please explain how to properly change the button's text. Preferably with examples for different button states (
No answers yet.
Log in to leave an answer
Blogs