Luke Patterson Luke Patterson - 6 months ago 3348
iOS Question

Swift 2.2 Selector with multiple arguments - actually passing in

I want to use a selector, but I need to pass in the arguments
I understand the syntax follows:

#selector(class.method(_:paramName:))


but i need to actually pass in parameters. How do I do this?

Here's my attempt:

exploreTap = UITapGestureRecognizer(target: self, action: #selector(MainViewController.showViewWithIdentifier(_:exploreView,id:"explore")))

Answer

You cannot pass parameters to selectors, selector is just a method name, nothing else. You are not calling the method so you cannot pass parameters. Just call the necessary code inside your tap handler.

func onTap() {
   MainViewController.showViewWithIdentifier(exploreView, id:"explore")
}

and then

UITapGestureRecognizer(target: self, action: #selector(onTap))
Comments