gpt4 book ai didi

ios - Swift 2 - for 循环创建多个 UIButton

转载 作者:行者123 更新时间:2023-11-29 10:24:08 32 4
gpt4 key购买 nike

我有一个 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)
}

如何将值传递给操作函数,以便应用可以看到按下了哪个按钮?

最佳答案

您可以使用标签值:

       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)

/// set tag
customButtonPlayVideoUser.tag = index
}

然后

    func CustomUserVideobtnTouched(sender: UIButton){

print(sender.tag)
}

关于ios - Swift 2 - for 循环创建多个 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33300994/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com