Swift 5.9
To change the title of a button in Swift, you can access the title property of the button and set it to the desired text. Here's an example:
override func viewDidLoad() {
super.viewDidLoad()
// Set the initial title of the button
myButton.setTitle("Initial Title", for: .normal)
}
and in IBAction
// Change the title of the button when a certain action occurs
myButton.setTitle("New Title", for: .normal)
In this example, we have a button called myButton. In viewDidLoad(), we set the initial title of the button using setTitle(_:for:) with the .normal control state. Then, in the changeButtonTitle function (which you can associate with a user action, like a button tap), we change the title of the button to "New Title." You can modify the text as needed to change the button's title to whatever you want in response to different events in your app.
UIControlState
. e.g.forState: .Normal
– Wileywilfong