Do gesture recognizers retain the target?

151 views
Skip to first unread message

Simon Harris

unread,
May 13, 2012, 8:54:24 PM5/13/12
to cocoah...@googlegroups.com
I noticed a bunch of code I have that removes gesture recognizers in viewWillDisappear. I can't recall why I did it but I'm wondering if it was in order to avoid a retain cycle. Now I'm questioning if that could even be possible. Is it necessary to remove gesture recognizers? Do they in fact retain their targets?

Thanks,
Simon

Chris Miles

unread,
May 13, 2012, 9:13:55 PM5/13/12
to cocoah...@googlegroups.com, Chris Miles
Hi Simon,

A view retains any gesture recognizers added to it. The gesture recognizers do not retain the views. Although they provide a reference to the view they were added to.

When a view is dealloc'd I would expect it to release any added gesture recognizers.

I don't see a reason to explicitly remove the gesture recognizers in viewWillDisappear, unless they are being added in a related method like viewWillAppear and the developer was ensuring that duplicate recognizers were not being added to views.

Cheers,
Chris

Simon Harris

unread,
May 13, 2012, 9:17:39 PM5/13/12
to cocoah...@googlegroups.com
Hi Chris,

Thanks for responding so quickly.

On 14/05/2012, at 11:13 AM, Chris Miles wrote:

> Hi Simon,
>
> A view retains any gesture recognizers added to it. The gesture recognizers do not retain the views. Although they provide a reference to the view they were added to.

I was also interested if the targets were retained but if they are release by the view then that wouldn't be a problem anyway, I guess.

> I don't see a reason to explicitly remove the gesture recognizers in viewWillDisappear, unless they are being added in a related method like viewWillAppear and the developer was ensuring that duplicate recognizers were not being added to views.

Right, I am adding them in viewWillAppear but again, I'm not exactly sure why I did it that way. I suspect I just blindly copied an apple example way back when I was first learning this stuff.

Chris Miles

unread,
May 13, 2012, 9:28:22 PM5/13/12
to cocoah...@googlegroups.com, Chris Miles
Hi Simon,

On 14/05/2012, at 11:17 AM, Simon Harris wrote:

>> I don't see a reason to explicitly remove the gesture recognizers in viewWillDisappear, unless they are being added in a related method like viewWillAppear and the developer was ensuring that duplicate recognizers were not being added to views.
>
> Right, I am adding them in viewWillAppear but again, I'm not exactly sure why I did it that way. I suspect I just blindly copied an apple example way back when I was first learning this stuff.

I can't think of any specific reason to implement that strategy over adding recognizers in viewDidLoad, for example. Other than for a more aggressive memory saving strategy. On the other hand, there's nothing wrong with it if it is working for you.

Cheers,
Chris

Simon Harris

unread,
May 13, 2012, 11:05:59 PM5/13/12
to cocoah...@googlegroups.com, Chris Miles
>>> I don't see a reason to explicitly remove the gesture recognizers in viewWillDisappear, unless they are being added in a related method like viewWillAppear and the developer was ensuring that duplicate recognizers were not being added to views.
>>
>> Right, I am adding them in viewWillAppear but again, I'm not exactly sure why I did it that way. I suspect I just blindly copied an apple example way back when I was first learning this stuff.
>
> I can't think of any specific reason to implement that strategy over adding recognizers in viewDidLoad, for example. Other than for a more aggressive memory saving strategy. On the other hand, there's nothing wrong with it if it is working for you.

OK, thanks Chris

Reply all
Reply to author
Forward
0 new messages