Simple question here. I have a UIButton, currencySelector, and I want to programmatically change the text. Here's what I have:
简单的问题。我有一个UIButton, currencySelector,我想以编程方式改变文本。这就是我有:
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?
Xcode给出了错误的“预期声明”。我做错了什么?我如何更改按钮的文本?
7 个解决方案
#1
439
In Swift 3:
在斯威夫特3:
button.setTitle("Button Title",for: .normal)
Otherwise:
否则:
button.setTitle("Button Title", forState: UIControlState.Normal)
#2
58
Just a clarification for those new to Swift and iOS programming. Below line of code:
这只是对Swift和iOS编程的一个澄清。下面的代码:
button.setTitle("myTitle", forState: UIControlState.Normal)
only applies to IBOutlets
, not IBActions
.
只适用于iboutlet,而不适用ibaction。
So, if your app is using a button as a function to execute some code, say playing music, and you want to change the title from Play
to Pause
based on a toggle variable, you need to also create an IBOutlet
for that button.
因此,如果您的应用程序使用一个按钮作为函数来执行一些代码,比如播放音乐,并且您想要根据toggle变量将标题从播放更改为暂停,您还需要为该按钮创建一个IBOutlet。
If you try to use button.setTitle
against an IBAction
you will get an error. Its obvious once you know it, but for the noobs (we all were) this is a helpful tip.
如果你尝试使用按钮。对IBAction进行setTitle会得到一个错误。一旦你知道了这一点,这是显而易见的,但对于noobs(我们都知道)来说,这是一个有用的建议。
#3
5
Swift 3:
斯威夫特3:
Set button title:
设置按钮标题:
//for normal state:
my_btn.setTitle("Button Title", for: .normal)
// For highlighted state:
my_btn.setTitle("Button Title2", for: .highlighted)
#4
4
Swift 3.0
斯威夫特3.0
// Standard State
myButton.setTitle("Title", for: .normal)
#5
2
Swift 4:
for state: UIControlState in [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved] {
button.setTitle(NSLocalizedString("Title", comment: ""), for: state)
}
#6
1
Swift 3
斯威夫特3
When you make the @IBAction:
当您创建@IBAction时:
@IBAction func btnAction(_ sender: UIButton) {
sender.setTitle("string goes here", for: .normal)
}
This sets the sender as UIButton (instead of Any) so it targets the btnAction as a UIButton
它将sender设置为UIButton(而不是Any),因此它将btnAction设置为UIButton
#7
0
Swift 3
斯威夫特3
let button: UIButton = UIButton()
button.frame = CGRect.init(x: view.frame.width/2, y: view.frame.height/2, width: 100, height: 100)
button.setTitle(“Title Button”, for: .normal)
#1
439
In Swift 3:
在斯威夫特3:
button.setTitle("Button Title",for: .normal)
Otherwise:
否则:
button.setTitle("Button Title", forState: UIControlState.Normal)
#2
58
Just a clarification for those new to Swift and iOS programming. Below line of code:
这只是对Swift和iOS编程的一个澄清。下面的代码:
button.setTitle("myTitle", forState: UIControlState.Normal)
only applies to IBOutlets
, not IBActions
.
只适用于iboutlet,而不适用ibaction。
So, if your app is using a button as a function to execute some code, say playing music, and you want to change the title from Play
to Pause
based on a toggle variable, you need to also create an IBOutlet
for that button.
因此,如果您的应用程序使用一个按钮作为函数来执行一些代码,比如播放音乐,并且您想要根据toggle变量将标题从播放更改为暂停,您还需要为该按钮创建一个IBOutlet。
If you try to use button.setTitle
against an IBAction
you will get an error. Its obvious once you know it, but for the noobs (we all were) this is a helpful tip.
如果你尝试使用按钮。对IBAction进行setTitle会得到一个错误。一旦你知道了这一点,这是显而易见的,但对于noobs(我们都知道)来说,这是一个有用的建议。
#3
5
Swift 3:
斯威夫特3:
Set button title:
设置按钮标题:
//for normal state:
my_btn.setTitle("Button Title", for: .normal)
// For highlighted state:
my_btn.setTitle("Button Title2", for: .highlighted)
#4
4
Swift 3.0
斯威夫特3.0
// Standard State
myButton.setTitle("Title", for: .normal)
#5
2
Swift 4:
for state: UIControlState in [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved] {
button.setTitle(NSLocalizedString("Title", comment: ""), for: state)
}
#6
1
Swift 3
斯威夫特3
When you make the @IBAction:
当您创建@IBAction时:
@IBAction func btnAction(_ sender: UIButton) {
sender.setTitle("string goes here", for: .normal)
}
This sets the sender as UIButton (instead of Any) so it targets the btnAction as a UIButton
它将sender设置为UIButton(而不是Any),因此它将btnAction设置为UIButton
#7
0
Swift 3
斯威夫特3
let button: UIButton = UIButton()
button.frame = CGRect.init(x: view.frame.width/2, y: view.frame.height/2, width: 100, height: 100)
button.setTitle(“Title Button”, for: .normal)