我已经使用for循环以编程方式将按钮添加到uistackview中,但是我希望每个按钮都具有自己的独立侦听器,因此我不认为它具有逻辑性。我试图添加一个#selector,但是显然它们都调用相同的方法。
for imageName in imagesArray1{
let imageButton: UIButton = {
let button = UIButton()
let myUIImage = UIImage(named: imageName as! String)
button.setImage(myUIImage, for: .normal)
button.contentMode = .scaleAspectFit
button.tag = buttonTag
buttonTag += 1
button.addTarget(self, action: #selector(handleSelectedAvatar(_:)), for:.touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
stackView1.addArrangedSubview(imageButton)
imageButton.widthAnchor.constraint(equalTo: stackView1.widthAnchor, multiplier: 1/4).isActive = true
imageButton.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.width/4).isActive = true
}
答案 0 :(得分:2)
有2种解决方案。
1)对所有按钮使用单个选择器:
button.tag = some_tag (Int)
button.addTarget(self, action: #selector(handleSelectedAvatar(_:)), for: .touchupInside)
handleSelectedAvatar
实现:
@objc func handleSelectedAvatar(_ sender: UIButton) {
switch sender.tag {
case 0: // Do something with UIButton with tag 0
case ...
}
}
2)使用选择器数组:
let selectors = [#selector(handleSelectedAvatar), ...]
比为按钮设置选择器:
button.addTarget(self, action: selectors[your_index], for: .touchupInside)
答案 1 :(得分:2)
首先,您应该编辑您的问题,只需发布代码,如果发现难以缩进或正确格式化,我会为您编辑。
我可以想出多种方式来做自己想做的事。最简单的方法是使用按钮的 tag 属性。此标记应帮助您访问图像数组中的任何对象(或您的情况下的UIImage
)。根据循环的当前索引分配标签,然后每个按钮将具有一个公共的选择器方法。
import UIKit
class ViewController: UIViewController {
internal lazy var stackView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [])
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .fillEqually
stackView.spacing = 10
return stackView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
// StackView Autolayout
self.view.addSubview(self.stackView)
self.stackView.translatesAutoresizingMaskIntoConstraints = false
self.view.addConstraint(NSLayoutConstraint(item: self.stackView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1.0, constant: 100.0))
self.view.addConstraint(NSLayoutConstraint(item: self.stackView, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 20.0))
self.view.addConstraint(NSLayoutConstraint(item: self.stackView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: 20.0))
// Add Buttons
for index in 1...10 {
let button = UIButton(type: .custom)
button.backgroundColor = UIColor.gray
button.setTitle("Button #\(index)", for: .normal)
button.addTarget(self, action: #selector(self.buttonHandler(_:)), for: .touchUpInside)
button.tag = index
self.stackView.addArrangedSubview(button)
}
}
@objc private func buttonHandler(_ button: UIButton) {
print("Tapped button #:\(button.tag)")
}
}
我希望这会有所帮助!