目标C:ViewController中的GestureRecognizer->保持周期? [英] Objective C: GestureRecognizer in ViewController --> retain cycle?

查看:70
本文介绍了目标C:ViewController中的GestureRecognizer->保持周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个ViewController(VC)的头文件中定义了以下属性:

I have the following property defined in the header file of one of my ViewControllers (VC):

@property(nonatomic, retain) IBOutlet UIImageView *articleImageView1;

在VC的实现中,在方法viewDidLoad中,我将TapRecognizers附加到以下属性:

In the implementation of the VC, in the method viewDidLoad, I attach TapRecognizers to these properties:

UITapGestureRecognizer *captureImage1TapRecognizer = [[UITapGestureRecognizer alloc]
                                                      initWithTarget:self
[self.articleImageView1 addGestureRecognizer:captureImage1TapRecognizer];

VC坚决保留UIImageView.

  1. UIImageView是否也通过GestureRecognizer强烈包含VC?
  2. 这是保留周期的经典示例吗?
  3. 如果是这样,这是否是正确的解决方案?

  1. Does this UIImageView also contain the VC strongly through the GestureRecognizer?
  2. Is this then a classical example of a retain cycle?
  3. If so, would this be correct solution?

-(void)viewDidUnload {
     ...
    [self setArticleImageView1:nil];
     ...
    [super viewDidUnload];
}

推荐答案

您在这里没有很强的参考周期.这是一个应该可以正常工作的解决方案.

You don't have a strong reference cycle here. This is a solution that should work just fine.

视图控制器对UIImageView有很强的引用. UIImageView将其手势识别器保存在一个数组中(也有强引用),但是手势识别器不保存目标/操作或强烈委托.

The view controller has a strong reference to the UIImageView. The UIImageView holds its gesture recognizers in an array (strong reference as well), but the gesture recognizer does not hold target/action or delegate strongly.

您可以通过弱引用保留UIImageView,但是由于iOS 6视图不再被卸载,因此对内存管理没有任何影响.

You may hold onto the UIImageView via a weak reference, but since iOS 6 views are not unloaded anymore, so it won't make any difference for memory management.

这篇关于目标C:ViewController中的GestureRecognizer->保持周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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