添加调用另一个类中的方法的点击手势 [英] Adding a tap Gesture that calls a method in another class

查看:23
本文介绍了添加调用另一个类中的方法的点击手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是我想为 UIImageView 添加一个点击手势,当用户触摸时,调用在另一个类中实现的方法(不是包含 UIImageView 的同一个类)代码>).

Simply I want to add a tap gesture for a UIImageView that when the user touches, calls a method that is implemented in another class (not the same class that contains the UIImageView).

我可以这样做吗?如果可以,我该怎么做?

推荐答案

你可以这样做.但是您需要目标类的实例(方法将在其中执行的类)作为委托或手势添加类中的某些内容.

You can do that. But you need the instance of the target class (class in which method is going to execute) as delegate or something in the gesture adding class.

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self.delegate action:@selector(doSomething)];  

这里的委托将是您在转到该类之前必须设置的目标类的实例.

Here delegate will be the instance of the target class that you have to set before going to that class.

编辑

我再解释一下.假设您有两个视图控制器类 VCA 和 VCB.您想通过点击手势从 VCB 调用 VCA 中的方法.

I will explain a little more. Suppose you have two view controller classes VCA and VCB. you want to call a method in VCA from VCB through a tap gesture.

在 VCB.h

@property (nonatomic,assign)VCA *delegate;

在 VCB.m

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self.delegate action:@selector(doSomething)]; 

在 VCA.m

您将展示/推送 VCB

You will present/ push VCB

 VCB * viewController = [[VCB alloc]init];
 viewController.delegate = self;
 // push or present viewController

这篇关于添加调用另一个类中的方法的点击手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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