如何从另一个类调用@selector方法 [英] how to call @selector method from another class

查看:187
本文介绍了如何从另一个类调用@selector方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从另一个类调用@selector方法? 例如,我创建一个方法"bannerTapped:",然后从"myViewController.m"类中调用它.

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:

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:

anotherClass.m :

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

已更新:

viewController.m:

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:

anotherClass.h:

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

anotherClass.m:

anotherClass.m:

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


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

推荐答案

是的,像这样

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

Target是您要将事件发送到的类实例.

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

编辑

由于您的问题远比您要问的要复杂得多,因此请以后再学习发布所有代码.长话短说,你不能这样做:

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:)];

}

此方法完成后,ac将立即从内存中释放,因为创建该示波器的作用域已消失.在这里使用ARC没什么区别.

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)使其成为类方法,这意味着您无法创建ac的实例变量,从某种意义上讲,这是您要尝试的操作,但是您仍在错误的位置创建它.
  • 我会怀疑(只是根据代码猜测)您认为ac指向当前在导航堆栈中的viewController. ac是该类的全新副本.您创建了一个新副本,该副本不会在任何地方显示或在任何地方使用,并在该方法完成后立即死亡.
  • +(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.

请进一步研究Objective-C和面向对象的编程,然后重试.

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

这篇关于如何从另一个类调用@selector方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆