答案 0 :(得分:1)
addTarget是android中的按钮操作侦听器的功能。
dynamicButtonInstance.addTarget(self,action:#selector(handleRegister),for:.touchUpInside)
handleRegister是类中的一个函数。
创建按钮
let button = UIButton(type: .system)
button.backgroundColor = UIColor(r: 50 , g: 80, b: 130)
button.setTitle("Register", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitleColor(.white, for: .normal)
button.addTarget(self, action:#selector(handleRegister),
for:.touchUpInside)
答案 1 :(得分:1)
Hi you can achieve this by using collectionView(is the efficient approach) or by adding the buttons
So while adding button just add a tag of button with +1 and when user click the button just fetch button tag and you will get which button is clicked.
Please find below sample
var prevBtn = 0
func addButton()
let button = UIButton()
button.tag = prevBtn.tag + 1
button.addTarget(self, action:#selector(handleButtonAction),
for:.touchUpInside)
prevBtn = button.tag
}
func handleButtonAction(button: UIButton){
print(button.tag)
}
Thanks