How can I access the name of a selector connected to a UIGestureRecognizer?

I have a gesture recognizer attached to the view and I would like to be able to unit test which method it calls when the tap occurs. My gesture recognizer is created like this ...

- (void)setupMyView { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myViewTapped)]; self.myView.userInteractionEnabled = YES; [self.myView addGestureRecognizer:tap]; } 

How can I access the selector name (myViewTapped) that gets called when the tap appears?

Thank you very much for your wisdom!

+4
source share
2 answers

Unfortunately, neither UIGestureRecognizer nor UITapGestureRecognizer discloses this information.

UIControl , for example, provides allTargets and allControlEvents , which is basically what you are looking for, but unfortunately not available for UIGestureRecognizer

As a result, I don’t think what you want without using private methods.

+4
source

Use this inside the myViewTapped method,

 NSLog(@"method name: %@", NSStringFromSelector(_cmd)) 

It can also print a method name,

 NSLog(@"%s", __PRETTY_FUNCTION__); 

A source:

+2
source

Source: https://habr.com/ru/post/1444199/


All Articles