これはよくある問題ですが、私自身は解決策を見つけることができませんでした。クラスに送信されたセレクタが認識されない
ここでのコードは次のとおりです。
class ButtonViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
}
func exmp(sender: UIButton) {
print("hello world")
}
let button: UIButton = {
let but = UIButton(frame: CGRect(x: 33, y: 33, width: 33, height: 33))
but.setTitle("-", for: .normal)
but.titleLabel?.textColor = UIColor.white
but.layer.cornerRadius = 10
but.backgroundColor = UIColor.red
but.addTarget(ButtonViewController.self, action: #selector(ButtonViewController.exmp(sender:)), for: .touchDown)
return but
}
}
問題: 赤いボタンが表示されますが、私はそれをクリックしたときに、私はエラー「クラスに送信された認識されないセレクター」を取得します。
ご協力いただきましてありがとうございます。ありがとう。
アドオン '@ objc' FUNCをexmp'の前に(送信者:UIButton) ' – NSDmitry
この動作するはずbut.addTarget(ButtonViewController.self、アクション:#selector(exmp :)、 for:.touchDown) –
は、インターフェイスビルダーでこのボタンを定義するだけです(必要に応じて必要になるまで非表示にします)。 – Alexander