roc*_*101 207 xcode uibutton ios swift
简单问题在这里.我有一个UIButton,currencySelector,我想以编程方式更改文本.这就是我所拥有的:
currencySelector.text = "foobar"
Run Code Online (Sandbox Code Playgroud)
Xcode给出了错误"预期声明".我做错了什么,如何让按钮的文字改变?
Gal*_*rom 549
在Swift 3中:
button.setTitle("Button Title",for: .normal)
Run Code Online (Sandbox Code Playgroud)
除此以外:
button.setTitle("Button Title", forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)
Ben*_*rix 71
只是对Swift和iOS编程新手的澄清.下面的代码行:
button.setTitle("myTitle", forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)
只适用于IBOutlets,而不是IBActions.
所以,如果您的应用使用一个按钮的功能,以执行一些代码,说播放音乐,并希望标题从改变Play到Pause基于一个切换变量,您还需要创建一个IBOutlet该按钮.
如果你试图使用button.setTitle反对,IBAction你将收到一个错误.一旦你知道它就很明显,但对于新手(我们都是),这是一个有用的提示.
Dmi*_*try 11
for state: UIControlState in [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved] {
button.setTitle(NSLocalizedString("Title", comment: ""), for: state)
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
设置按钮标题:
//for normal state:
my_btn.setTitle("Button Title", for: .normal)
// For highlighted state:
my_btn.setTitle("Button Title2", for: .highlighted)
Run Code Online (Sandbox Code Playgroud)
Swift 3.0
// Standard State
myButton.setTitle("Title", for: .normal)
Run Code Online (Sandbox Code Playgroud)
归属时更改标题有些不同:
我只是遇到一个问题:如果您的UIButton带有属性标题,则必须使用:
my_btn.setAttributedTitle(NSAttributedString(string: my_title), for: my_state)
Run Code Online (Sandbox Code Playgroud)
如果同时为按钮设置标题和属性标题,则该按钮比该按钮更喜欢使用属性标题。
我有一个归属的标题,我试图对其设置setTitle,但没有效果...
| 归档时间: |
|
| 查看次数: |
213886 次 |
| 最近记录: |