Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call selector on static methods from separate class

let's say I want to create the following gesture recognizer

UITapGestureRecognizer * c1 = [[UITapGestureRecognizer alloc] 
          initWithTarget:self 
          action:@selector([[MyGestureRecognizer ViewWasClicked1:]]; // error 
[c1 setNumberOfTapsRequired:1];
[c1 setNumberOfTouchesRequired:1];
[[self view] addGestureRecognizer:c1];

but I want to call the selector on a separate class. I have the method:

+ (void)ViewWasClicked1:(UITapGestureRecognizer *)sender {    

    NSLog(@"click1 mouse down");

}

in the class MyGestureRecognizer. is it possible to what am I looking for?

like image 416
Tono Nam Avatar asked May 05 '12 02:05

Tono Nam


2 Answers

The syntax is:

UITapGestureRecognizer * c1 = [[UITapGestureRecognizer alloc] 
      initWithTarget:[MyGestureRecognizer class]
      action:@selector(ViewWasClicked1:)]; // error
like image 141
MByD Avatar answered Oct 19 '22 18:10

MByD


To check for and call static methods, you can do this:

SEL staticMethodSelector = @selector(methodName);
if ([[ClassName class] respondsToSelector:staticMethodSelector]) {
    [[ClassName class] performSelector:staticMethodSelector];
}
like image 38
Jeremy Conkin Avatar answered Oct 19 '22 17:10

Jeremy Conkin