addTarget UIButton 不起作用



我正在尝试以编程方式创建一个按钮。当它被按下时,我希望它调用一个函数,但这不起作用。按钮完美出现。

func createSubject(name: String, color: UIColor, fromLoad: Bool){
createVC(name: name)
let button : UIButton = UIButton(frame: createButtonCGRect())
UIView .animate(withDuration: 0.5, delay: 0.1, options: .curveEaseIn, animations: ({
button.setTitle(name, for: .normal)
button.layer.cornerRadius = button.frame.width/2
button.backgroundColor = color
button.setTitle(name, for: .application)
button.isOpaque = false
//button.addTarget(self, action: #selector(self.presentVC(button:)), for: .touchUpInside)
//button.addTarget(self, action: #selector(self.deleteSubject(selector:)), for: .touchUpInside)
button.addTarget(self, action: #selector(self.addToWord), for: .touchUpInside)
button.setTitleColor(UIColor.white, for: .normal)
view!.addSubview(button)
}), completion: nil)
if (fromLoad == false){
save().saveSubject(button: button)
disappearConfiguration()
}else{
UIView .animate(withDuration: 0.5, animations: {
self.placeAddButton()
addButton?.isUserInteractionEnabled = true
addButton?.alpha = 1
})
}
}
@objc func addToWord(selector: UIButton!){
print("HOOOLAA")
}

谢谢大家

尝试以下代码并检查

button.addTarget(self,action:#selector(self.addToWord(_:)), forControlEvents:.TouchUpInside)

祝你好运。

最新更新