I have a Swift project where I want to attach a method to a UIButton's tap event. I have the following code:
class MyClass {
let myButton = UIButton(frame: CGRectMake(50, 50, 100, 50))
init() {
myButton.addTarget(self, #selector(self.didTap(_:)), forControlEvents: .TouchUpInside)
}
func didTap(sender: UIButton) {
print("Tapped")
}
}
XCode highlights my addTarget
line and says:
Argument of '#selector' refers to a method that is not exposed to Objective-C
If I add the @objc
prefix to my func didTap
like it suggests then everything works fine.
Do I have something enabled in my build settings which is causing this strange behaviour?
PS. I get this behaviour in 7.3.1. But if I try this in 7.2.1 it doesn't accept the #selector(method(_:))
syntax, and Selector("method:")
works fine.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…