Swift - How to call Selector() on method with arguments?
24,337
Solution 1
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(_:)))
Solution 2
You don't pass arguments in selectors. You only represent that there is one with a colon. Additionally, you don't have to use the Selector type at all. If you pass in a String literal, it is converted to a Selector for you.
gestureRecognizer.addTarget(self, action:"dismiss:")
Comments
-
Kosmetika almost 4 years
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")
andSelector("dismiss(nil)")
with no luck..Here the method I'm calling:
func dismiss(completion: (() -> Void)!) { self.dismissViewControllerAnimated(true, completion: completion) }