【发布时间】:2015-10-23 11:14:43
【问题描述】:
我有一个 for 循环,它根据用户注册的方式创建多个按钮。
按钮创建良好,我已向按钮添加了一个目标,该目标需要打印已按下哪个按钮并传递一个值。
目前为止:
func loadUserVideos() {
let userVideoNames = ["andrew", "john", "verya", "patry", "gabriy"]
let countUserNames = userVideoNames.count - 1
for index in 0...countUserNames {
//print("\(index) times 5 is \(index * 5)")
//UIBUTTON PLAY VIDEO USER
let customButtonPlayVideoUser = UIButton(frame: CGRectMake(145, 32, 64, 64))
let imageCustomUserBtn = UIImage(named: "video-play.png") as UIImage?
customButtonPlayVideoUser.setImage(imageCustomUserBtn, forState: .Normal)
customButtonPlayVideoUser.addTarget(self, action: "CustomUserVideobtnTouched:", forControlEvents:.TouchUpInside)
customViewUser.addSubview(customButtonPlayVideoUser)
}
}
func CustomUserVideobtnTouched(sender: UIButton, index: Int){
print(index)
}
如何将值传递给操作函数,以便应用查看按下了哪个按钮?
【问题讨论】:
-
您已经通过
sender引用了该按钮,但您也可以使用index(customButtonPlayVideoUser.tag = index) 标记该按钮