UIGestureRecognizer导致循环保留?

我在想,如果在手势识别器的initWithTarget:action:方法中将目标指定为self ,是否会导致循环保留? 由于self会保留self.gestureRecognizers的识别器,因此手势识别器也可以在initWithTarget:action保留self。

以下是解决scheme吗?

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

我完全理解,确认一些缺乏文档的东西会让人感到不安。 重要的是要注意,通过十多年的惯例,目标 – 行动关系从来没有strong 。 这里是相关的文档 。 注意说的部分:

控制对象不(也不应该)保留它们的目标。

MRC的方式“保留…”, strongstrong ……”

鉴于这种交互方式的文档化惯例,可以假设,如果UIGestureRecognizer的目标行为实现保留了对象,那么这种exception将会非常奇怪,以至于它会出现在它的文档中。


PS您不必担心引用控件在内存pipe理环境中保留目标的注释。 “内存pipe理”指的是Cocoa中的(现在被depricated的)垃圾收集。 MRC或ARC都不是内存pipe理的。

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

这不保留自己。 这也将在文档中注意到

您需要将识别器添加到视图。 该观点将保留识别器。 没有保留周期。

 [aView addGestureRecognizer:tapRec]; 

addGestureRecognizer: docs提到,视图保留了识别器


正如你使用ARC,这是你所要做的

 UIGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; [aView addGestureRecognizer:tapRec];