I'm learning Swift and need to call my method on tap, here is the code:
var gestureRecognizer = UITapGestureRecognizer()
myView.addGestureRecognizer(gestureRecognizer)
gestureRecognizer.addTarget(self, action: Selector(dismiss(nil)))
This returns error - Could not find an overload for init that accepts the supplied arguments
I also tried like Selector("dismiss:nil")
and Selector("dismiss(nil)")
with no luck..
Here the method I'm calling:
func dismiss(completion: (() -> Void)!) {
self.dismissViewControllerAnimated(true, completion: completion)
}
The solution to your problem is to pass the object that should run the selector method along with the selector to the initialisation of the ValueAnimator object. Also update the timerCallback() : @objc func timerCallback() { ... _ = target.
Use Selectors to Arrange Calls to Objective-C Methods In Objective-C, a selector is a type that refers to the name of an Objective-C method. In Swift, Objective-C selectors are represented by the Selector structure, and you create them using the #selector expression.
Just use the name of the method as a string:
gestureRecognizer.addTarget(self, action: "dismiss:")
Edit: In Swift 3.0 you will have to use the following syntax:
gestureRecognizer.addTarget(self, action: #selector(dismiss(_:)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With