以编程方式快速更改 UIButton 的文本

问题描述:

这里有一个简单的问题.我有一个 UIButton、currencySelector,我想以编程方式更改文本.这是我所拥有的:

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 给了我错误预期声明".我做错了什么,如何更改按钮的文本?

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

在 Swift 3、4、5 中:

In Swift 3, 4, 5:

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

否则:

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

还必须为 button 声明一个 @IBOutlet.

Also an @IBOutlet has to declared for the button.