UIGestureRecognizer导致循环保留? [英] UIGestureRecognizer causes circular retain?

查看:248
本文介绍了UIGestureRecognizer导致循环保留?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果您在手势识别器的 initWithTarget:action:方法中将目标指定为 self 你会造成循环保留吗?由于 self 会保留 self.gestureRecognizers 中的识别器,并且手势识别器也可以保留 initWithTarget:action

I was thinking, if you assign the target as self in the gesture recogniser's initWithTarget:action: method, will you cause a circular retain? Since self will retain the recogniser in self.gestureRecognizers and it's possible the gesture recogniser to also retain self in initWithTarget:action.

以下是解决方案吗?

__weak VRDrawer* weakSelf = self;
UIGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc] initWithTarget:weakSelf
                                                                      action:@selector(handleTap:)];


推荐答案

我完全理解,感到不安。重要的是要注意,在十多年的惯例中,目标 - 行动关系从不。以下是相关文档。请注意以下部分:

I completely understand confirming something with the lack of documentation can feel unsettling. The important thing to note is that, by over a decade of convention, target-action relationships are never strong. Here's the relevant docs. Note the part that says:


控制对象不会(也不应该)保留其目标。

Control objects do not (and should not) retain their targets.

保留...以MRC的方式说保存引用...

"Retain…" in the MRC way of saying "Holds a strong reference to…"

给定这是这种类型的交互的记载约定,可以安全地假设,如果 UIGestureRecognizer 的目标操作实现< 保留对象,该异常将会奇怪到它会显示在其文档中。

Given this is the documented convention for this type of interaction, it's safe to assume that, if UIGestureRecognizer's target-action implementation did retain objects, that anomaly would be strange enough that it would show up in its docs.

PS您不必担心指向在内存管理环境中保留目标的控件的注释。 内存管理是指Cocoa中的(现在已被废弃)垃圾回收。 MRC或ARC都不是内存管理的。

P.S. You don't have to worry about the note that refers to controls retaining targets in memory-managed environments. "Memory-managed" refers to the (now depricated) garbage collection in Cocoa. Neither MRC or ARC is memory-managed.

这篇关于UIGestureRecognizer导致循环保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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