如何在从数组循环时为UIButton分配唯一标记:
var buttonArray: NSMutableArray = ["one", "two", "Three"] // it is a mutable array but for illustration purposes only I put this....
for btnName in buttonArray {
let button = UIButton(frame: CGRectMake(0,0,100,100))
button.layer.masksToBounds = true
button.layer.cornerRadius = 20
button.setTitle("\(btnName)", forState: UIControlState.Normal)
//button.tag = ??????
print(button.tag)
}
答案 0 :(得分:1)
显然你可以注入一些反击。
bash
答案 1 :(得分:1)
或者,您可以使用enumerate()
。这将创建一个惰性序列,包含带有索引的元素对。
例如:
for (index, btnName) in buttonArray.enumerate() {
let button = UIButton(frame: CGRectMake(0,0,100,100))
...
button.tag = index
}