正确写法就是下面的代码(带参)
let btn : UIButton = UIButton.init(type: UIButton.ButtonType.custom)
self.addSubview(btn)
btn.frame = CGRect.init(x: 10, y: 0, width: 100, height: 30)
btn.setTitle("tor", for: UIControl.State.normal)
btn.setTitleColor(UIColor.black, for: UIControl.State.normal)
btn.addTarget(self, action: #selector(btnAction(_:)), for: .touchUpInside)
@objc func btnAction(_ sender:UIButton){
}
不带参
layerView.addTarget(self, action: #selector(goto), for: .touchUpInside)
@objc func goto() {
}
注意不是大写的“Selector”
是带#号的,而且带参的必须有下划线 #selector(btnAction(_:)
事件方法带参的必须有@objc 也必须有下划线 @objc func btnAction(_ sender:UIButton)