Passing a function to #selector

I get a function as a function parameter and want to set it to #selector. But I get an error message:

Argument of '#selector' cannot refer to a property 

I have the following function:

 private func addGestureRecognizerToItem(selector: () -> ()) { let labelGesture = UITapGestureRecognizer(target: self, action: #selector(selector)) let imageGesture = UITapGestureRecognizer(target: self, action: #selector(selector)) label.addGestureRecognizer(labelGesture) imageView.addGestureRecognizer(imageGesture) } 

Any ideas how to handle this?

+6
source share
2 answers

How about this?

 class ViewController: UIViewController { let label = UILabel() let imageView = UIImageView() override func viewDidLoad() { super.viewDidLoad() addGestureRecognizerToItem(#selector(test)) } func test() { } private func addGestureRecognizerToItem(selector: Selector) { let labelGesture = UITapGestureRecognizer(target: self, action: selector) let imageGesture = UITapGestureRecognizer(target: self, action: selector) label.addGestureRecognizer(labelGesture) imageView.addGestureRecognizer(imageGesture) } 

}

+7
source

This is not possible; rather, you can call the desired function from the return of the first function whose data you want to transfer to another. And your scenario may change according to your requirement.

0
source

All Articles