how to call @selector method from another class
Asked Answered
B

4

5

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 
   }
Beautify answered 13/2, 2014 at 15:19 Comment(0)
B
6

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.

Backwoods answered 13/2, 2014 at 15:22 Comment(6)
i did . and now it shows me a warning undeclared selector "bannerTappedBeautify
@Beautify have you declared bannerTapped in the .h file of that class and imported it into your viewController?Backwoods
i just declared bannerTapped method in .h of viewController class . but my app gets crashed now giving "unrecognized selector sent to instance " error.Beautify
@Beautify ....... yes so going back to my comment above, if your calling a selector on a class instance, the current file must know that method exists. You do this by declaring the method in anotherClass.h and import "anotherClass.h" into the viewController. Have you done this?Backwoods
@Beautify please edit your question, include the header for anotherClass.h, show how you declare the instance and update the code above to what you have nowBackwoods
@Beautify what error are you getting now and where are you running this line anotherClass *ac= [[anotherClass alloc]init]; if you are doing this in the init or viewDidLoad method just above where you create the gesture recogniser, I would bet this is getting released from memory before the tap is getting fired. If you aren't getting an error but its just not working. If this is your issue, you need to research object oriented programming and memory management much more as these are some of the fundamentals and this is far outside the scope of your questionBackwoods
B
1

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.

Barnaba answered 13/2, 2014 at 15:23 Comment(1)
Don't forget to import the relevant view controller.Marbles
A
1

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:)];
//...
Aliber answered 13/2, 2014 at 15:26 Comment(9)
and i didn't created property i just created an instance of "another class " and passed its instance in target .but its still shows warningBeautify
Add this to anotherClass.h file -(void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer; it should remove the warning.Aliber
warning vansihed ! and app crashed giving error "[nsarray bannerTapped:]unrecognized selector to instance "Beautify
It looks that you try to pass NSArray to the gesture recogniser not your custom class -anotherClass. Please post the line when you create the gesture recogniser, what is your target parameter? Have you declared bannerTapped: method in anotherClass.m file?Aliber
yes i declared bannerTapped: method in anotherClass.m file.Beautify
my target parameter is the instance of "anotherClass".Beautify
Can you show code when you allocate anotherClass and gesture recogniser. This code works for me, and your error says that you try to call bannerTapped: method on NSArray object not anotherClass one.Aliber
Is anotherClass subclass of UIViewController?Aliber
You have to add it to view hierarchy to make it works.Aliber
A
-1

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

Azalea answered 13/11, 2019 at 5:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.