以编程方式将目标操作添加到UIButton
我测试了很多样品,但没有一个对我有效。这是我的密码:以编程方式将目标操作添加到UIButton,uibutton,tvos,swift3,Uibutton,Tvos,Swift3,我测试了很多样品,但没有一个对我有效。这是我的密码: override func viewDidLoad() { super.viewDidLoad() let button = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 50)) button.backgroundColor = .green button.setTitle("Test Button", for: .normal)
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 200, height: 50))
button.backgroundColor = .green
button.setTitle("Test Button", for: .normal)
button.titleLabel?.sizeToFit()
button.addTarget(self, action: #selector(ViewController.buttonTapped(_:)), for: .touchUpInside)
self.view.addSubview(button)
}
func buttonTapped(_ sender: UIButton) {
print("Button tapped.")
}
我遗漏了什么?好吧,接下来的问题是.touchUpInside
。在tvOS中,它应该是.primaryActionTriggered
button.addTarget(self, action: #selector(ViewController.buttonTapped(_:)), for: .primaryActionTriggered)