gpt4 book ai didi

uibutton - 以编程方式向 UIButton 添加目标操作

转载 作者:行者123 更新时间:2023-12-01 10:33:10 27 4
gpt4 key购买 nike

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

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

我错过了什么?

最佳答案

嗯,关注 this answer问题出在 .touchUpInside .在 tvOS 中它应该是 .primaryActionTriggered .

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

关于uibutton - 以编程方式向 UIButton 添加目标操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39374164/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com