Swift、Xcode:单击时更改 UIButton 背景和文本 [英] Swift, Xcode: Changing UIButton Background and Text on Click
本文介绍了Swift、Xcode:单击时更改 UIButton 背景和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在单击时更改按钮的背景和文本,我尝试了几种 SO 解决方案,但没有奏效,您可以查看我在项目中尝试的内容:
2) 这样做:
@IBAction func 更改(发送方:UIButton){button.backgroundColor = UIColor.blueColor()button.setTitle("按钮标题", forState: UIControlState.Normal)}
I want to change the background and text of the button on click, I tried several SO solutions but they haven't worked, you can see what I tried in my project: https://github.com/jzhang172/modalTest
I tried debugging it by putting a simple print statement and it looks like it doesn't ever go to it.
解决方案
SOLUTION:
1) Create an IBAction from your UIButton and also an IBOutlet called button
.
EDIT: As per your request (How to trigger the even when the button is TOUCHED, not RELEASED?):
2) Do this:
@IBAction func changes (sender: UIButton) {
button.backgroundColor = UIColor.blueColor()
button.setTitle("Button Title", forState: UIControlState.Normal)
}
这篇关于Swift、Xcode:单击时更改 UIButton 背景和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文