Ios – Changing text of UIButton programmatically swift


Simple question here. I have a UIButton, currencySelector, and I want to programmatically change the text. Here's what I have:

currencySelector.text = "foobar"

Xcode gives me the error "Expected Declaration". What am I doing wrong, and how can I make the button's text change?

Best Solution

In Swift 3, 4, 5:

button.setTitle("Button Title", for: .normal)


button.setTitle("Button Title", forState: UIControlState.Normal)

Also an @IBOutlet has to declared for the button.