Swift 3 和无法识别的选择器发送到创建按钮的实例错误
Swift 3 and unrecognized selector sent to instance error for button creation
在我们开始使用 Swift 3 之后,我们遇到了“发送到实例的无法识别的选择器!”错误,无法在合理的时间找到解决方案。我们遇到的旧版本 Swift 的所有答案。这是我们提出的行动宣言
testbutton.addTarget(self, action: Selector("testButtonAction:"), for: UIControlEvents.touchDown)
这是 Xcode 的 "corrected" 版本,但它们都不起作用。
testbutton.addTarget(self, action: Selector(("testButtonAction:")), for: UIControlEvents.touchDown)
这是动作函数。
func testButtonAction(sender: UIButton!) {
print("Button tapped")
}
应该是这样的:
testButton.addTarget(self, action: #selector(YourViewController.testButtonAction(sender:)), for: UIControlEvents.touchDown)
Swift 3 的正确语法是:
testbutton.addTarget(self, action: #selector(testButtonAction(sender:)), for: UIControlEvents.touchDown)
这里的任何错误现在都将在编译时而不是 运行 时被拾取。
在我们开始使用 Swift 3 之后,我们遇到了“发送到实例的无法识别的选择器!”错误,无法在合理的时间找到解决方案。我们遇到的旧版本 Swift 的所有答案。这是我们提出的行动宣言
testbutton.addTarget(self, action: Selector("testButtonAction:"), for: UIControlEvents.touchDown)
这是 Xcode 的 "corrected" 版本,但它们都不起作用。
testbutton.addTarget(self, action: Selector(("testButtonAction:")), for: UIControlEvents.touchDown)
这是动作函数。
func testButtonAction(sender: UIButton!) {
print("Button tapped")
}
应该是这样的:
testButton.addTarget(self, action: #selector(YourViewController.testButtonAction(sender:)), for: UIControlEvents.touchDown)
Swift 3 的正确语法是:
testbutton.addTarget(self, action: #selector(testButtonAction(sender:)), for: UIControlEvents.touchDown)
这里的任何错误现在都将在编译时而不是 运行 时被拾取。