[英](swift) Anyone can help me to make bulk change to those buttons?
when user get into this page, I want to change the background of those button. 当用户进入此页面时,我想更改这些按钮的背景。 for example, when the level of user is 6, I want to change the top 6 background of buttons to a blue picture as first button. 例如,当用户级别为6时,我想将按钮的前6个背景更改为蓝色图片作为第一个按钮。 I don't wanna write a code one by one, so please help me to write a function, Thank you very much! 我不想一一编写代码,所以请帮助我编写一个函数,非常感谢!
You can add you buttons to the array in order of increasing level and do something like this: 您可以按级别递增的顺序将按钮添加到数组,并执行以下操作:
var buttons: [UIButton] // array of your buttons, next you need to init it
...
func setColorForUserLevel(userLevel: Int) {
if userLevel > buttons.count {
return
} else {
for i in 0...userLevel - 1 {
buttons[i].backgroundColor = UIColor.blueColor()
}
}
}
I create simple example project to show how to use it: 我创建一个简单的示例项目来展示如何使用它:
I have outlets with UIButton!
我有带UIButton!
商店UIButton!
type and buttons
object in code: 类型和buttons
对象在代码中:
And i test setColorForUserLevel
function in viewDidLoad
method: 我在viewDidLoad
方法中测试setColorForUserLevel
函数:
override func viewDidLoad() { super.viewDidLoad() 覆盖func viewDidLoad(){super.viewDidLoad()
buttons = [button1, button2, button3] setColorForUserLevel(2) } button = [button1,button2,button3] setColorForUserLevel(2)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.