嗨,對于這個問題,我找到了有關如何以編程方式創建按鈕的答案?但是仍然面臨錯誤:“'#selector' 的引數不能參考本地函式 'plusOne(sender:)'”和“@objc 只能與類成員、@objc 協議和類的具體擴展一起使用”。如果你能建議。
let button = UIButton()
button.frame = CGRect(x: 150, y: 300, width: 60, height: 60)
button.setTitle("Click", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.addTarget(self, action: #selector(plusOne), for: .touchUpInside)
self.view.addSubview(button)
@objc func plusOne(sender: UIButton!) {
self.count = 1
self.label.text = "\(self.count)"
}
uj5u.com熱心網友回復:
你遇到的問題是你已經嵌套了@objc func plusOne(sender: UIButton!)inside viewDidLoad(這就是為什么我問了關于范圍的最初問題)。您需要將其移至類范圍方法。
override func viewDidLoad() {
// all the usual stuff...
let button = UIButton()
button.frame = CGRect(x: 150, y: 300, width: 60, height: 60)
button.setTitle("Click", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.addTarget(self, action: #selector(plusOne), for: .touchUpInside)
self.view.addSubview(button)
}
@objc func plusOne(sender: UIButton!) {
self.count = 1
self.label.text = "\(self.count)"
}
uj5u.com熱心網友回復:
方法的名稱是plusOne(sender:),引數標簽是名稱的一部分
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435119.html
