【问题标题】:Programmatically addTarget action to UIButton以编程方式将目标操作添加到 UIButton
【发布时间】:2016-09-07 15:40:18
【问题描述】:

我测试了大量样本,但没有一个对我有用。这是我的代码:

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.")
}

我错过了什么?

【问题讨论】:

    标签: uibutton tvos swift3


    【解决方案1】:

    好吧,在this answer 之后,问题在于.touchUpInside。在 tvOS 中应该是 .primaryActionTriggered

    button.addTarget(self, action: #selector(ViewController.buttonTapped(_:)), for: .primaryActionTriggered)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      相关资源
      最近更新 更多