如何以编程方式向 StackView 中的每个按钮添加单独的 selectors/listeners swift 4.2

how to add separate selectors/listeners to each buttons in StackView programmatically swift 4.2

我已经使用 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
    }

有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)

首先,您应该编辑您的问题,只需 post 代码,如果您发现难以正确缩进或格式化,我会为您编辑。

我可以想到多种方法来做您想做的事。最简单的是使用按钮的 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)")
    }
}

希望对您有所帮助!