I've added buttons into uistackview programmatically with for loop but I want each button have it's own separate listeners and I can't think up a logic for it. I've tried to add a #selector but obviously they all call the same method.
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
}