سؤال

is it possible to call @selector methods from another class ? for example i make a method "bannerTapped:" and call it from "myViewController.m" class.

myviewcontroller.m :

anotherClass *ac= [[anotherClass alloc]init];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:ac action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
cell.conversationImageView.tag = indexPath.row;
[cell.conversationImageView addGestureRecognizer:singleTap];
[cell.conversationImageView setUserInteractionEnabled:YES];

anotherClass.m :

-(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
    //do something here 
}

updated :

viewController.m:

 #import "anotherClass.h"



 +(viewcontroller *)myMethod{
 // some code...

 anotherClass *ac= [[anotherClass alloc]init];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:ac   action:@selector(bannerTapped:)];

}

anotherClass.h:

-(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer;

anotherClass.m:

-(void)Viewdidload{
    [viewController myMethod];
     }


   -(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
      //do something here 
   }
هل كانت مفيدة؟

المحلول

Yes, like this

initWithTarget:anotherClassInstance action:@selector(bannerTapped:)];

The Target is the class instance you want to send the event to.

EDIT

Please learn to post all of your code in future as you question is FAR more complex than you have asked. Long story short you can't do this:

+(viewcontroller *)myMethod{

   anotherClass *ac= [[anotherClass alloc]init];

   UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:ac   action:@selector(bannerTapped:)];

}

As soon as this method finishes, ac will be released form memory as the scope it was created in is now gone. Using ARC makes no difference here.

You need to understand some different things here:

  • +(void) makes this a class method, meaning you can't create an instance variable of ac which is what you are trying to do in some sense, but you are still creating it in the wrong place.
  • I would suspect (just guessing based on the code) that you think ac is pointing to a viewController that is currently in the navigation stack. ac is a brand new copy of that class. You have created a new copy that is not displayed anywhere or used anywhere and dies as soon as that method has finished.

My first piece of code answers the question you asked, that is how you call a selector from another class. Your issue now is that you don't understand the flow of objects, memory management, class instances and class methods vs instance methods.

Please study objective-c and object oriented programming more and try this again.

نصائح أخرى

In the following line:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:myViewcontroller action:@selector(bannerTapped:)];

Whatever you specify as the target is the object that the bannerTapped: selector will be called on. So you just need to supply an instance of anotherClass in that parameter. Most of the time people will have the target be the view controller that contains the view they're adding the recognizer to, but this isn't necessary.

Yes you can the target parameter from

[[UITapGestureRecognizer alloc]initWithTarget: action:];

specify the class you want to run the selector to. Just one thing you should keep in mind is that your anotherClass needs to be allocated and it should keep strong reference. So the best way is create property:

@property (nonatomic, strong) anotherClass *myClass;

And make sure the class is allocated before you pass it to gesture recogniser, for example:

self.myClass = [[anotherClass alloc[init];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self.myClass action:@selector(bannerTapped:)];
//...

Looks like you've missed the ':' at the end of the name. @selector(bannerTapped:)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top