我已经制作了像这样的UIButtons的集合
@IBOutlet var btnChannle1:UIButton!
@IBOutlet var btnChannle2:UIButton!
@IBOutlet var btnChannle3:UIButton!
@IBOutlet var btnChannle4:UIButton!
@IBOutlet var btnChannle5:UIButton!
@IBOutlet var btnChannle6:UIButton!
@IBOutlet var btnChannle7:UIButton!
@IBOutlet var btnChannle8:UIButton!
@IBOutlet var channlesCollection:Array<UIView>!
override func viewDidLoad() {
super.viewDidLoad()
channlesCollection.append(btnChannle1)
channlesCollection.append(btnChannle2)
channlesCollection.append(btnChannle3)
channlesCollection.append(btnChannle4)
channlesCollection.append(btnChannle5)
channlesCollection.append(btnChannle6)
channlesCollection.append(btnChannle7)
channlesCollection.append(btnChannle8)
}
现在我想遍历上面的集合并想要这样的东西
for item in channlesCollection{
// Set Button Text Line to 2
// Set Button Text Like this in two line
/*
01
CNN
02
BBC
03
PTV ...
*/
}
但是当我尝试这样的时候。
for item in channlesCollection{
item.setTitle("Test", forState: UIControlState.Normal)
}
错误引发UIView没有名为setTitle的成员。
答案 0 :(得分:2)
您需要将项目输入为UIButton
。这是代码:
for item in channlesCollection{
var button:UIButton = item as UIButton
button.setTitle("Test", forState: UIControlState.Normal)
}
答案 1 :(得分:1)
您拥有UIView
数组,但他们确实没有setTitle
方法。您可以创建UIButton
类型的集合。或者将当前的收藏项目投射到按钮:
for item in channlesCollection {
if var button = item as? UIButton {
button.setTitle("Test", forState: UIControlState.Normal)
}
}
答案 2 :(得分:0)
UIView
没有成员函数setTitle
,但UIButton
有,这可能是您想要的。
您已将channlesCollection
声明为UIView
的数组,为什么不将其作为UIButton
的数组?
var channlesCollection: [UIButton]!