10

What is the correct way of attaching and removing a UIGestureRecognizer on a view?

I usually create and attach UIGestureReconizers in -viewDidLoad. Shall I use removeGestureRecognizer in the -viewDidUnload? Or are they automatically removed?

For example is it a better way to create a UIGestureRecognizer property and initialize it in the viewController's init method and then attach/remove this gestureRecognizer property in -viewDidAppear/-viewDidDisappear?

(Note I'm using ARC)

Thanks.

3
  • I have already skimmed through that but did not find the answer to my question.
    – mrd3650
    Apr 11, 2012 at 11:01
  • @TheLion : "Sorry, that page cannot be found."
    – Colas
    May 3, 2015 at 14:26
  • Sorry apple has changed it so here it is the new URL for the same resource. Thanks for point it out @Colas
    – The iOSDev
    May 14, 2015 at 9:31

3 Answers 3

10

Essentially you can create, add, and remove gesture recognizers when it makes sense. And "when it makes sense" varies depending on a few things, but usually just on whether you will keep them as ivars. With ARC the key is managing strong references; With ARC ivars are strong by default.

So let's say you add a tap recognizer. In this scenario you don't need to access the recognizer again. I would create it like this:

-(void)viewDidLoad{
    [super viewDidLoad];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self.view addGestureRecognizer:tap];
}

With this scenario, you need do nothing else to manage this recognizer. The view system is holding strong reference to it. All you do is sit back and wait for it to call handleTap:. When the view unloads this tap recognizer will be deallocated.

This scenario would also not be a good one to add this recognizer in viewWillAppear: since it's not hard to imagine a series of events that would lead to multiple recognizers being created.

So logically if you want to add a recognizer in the appearance methods you would have to hold some sort of reference to them in an ivar. But again that is not always a bad idea. Generally the reason you would want reference to your recognizers is to help in the logic of <UIGestureRecognizerDelegate> methods, if they are used.

I personally would never add a recognizer in the initWith.. method for attachment later. The only advantage is that any recognizer you create there would be kept around the entire life of the view controller. But since a recognizer is pretty clearly part of the view system this kind of muddies-up the MVC that Cocoa is big on. Also in a low memory condition view of controllers on the navigation stack are purged, but in this case the recognizer might remain using memory(not that there huge objects) for no real reason.

You certainly could add a recognizer in an view(Will/Did)Appear: method and remove it in view(Will/Did)Disappear:. Not my preference. I don't think that just because a modal view is presented and removed or a navigation push/pop has happened that I should remove and re-add my recognizers.

I would recommend nearly always using viewDidLoad to instantiate and attach your recognizers. Just remember to nil-out any strong references to them in viewDidUnload to help in a low memory situation. And again with ARC there is no need to do anything with ivars in dealloc.

1
  • Thanks NJones for the detailed answer! The reason for attaching/detaching Recognizers in View(will/did)Load and Unload was purely for memory reasons (so that when view is not on screen, the memory is release and assigned back when it is on screen) but from what you say, it does not make that much of a difference.
    – mrd3650
    Apr 16, 2012 at 10:53
0

viewDidUnload is called only in low-memory conditions, and is not guaranteed to be called at all. Therefore, any gesture recognisers removed in viewDidUnload should also be removed in dealloc. EIther method you mention is fine.

0

ok then viewWillAppear:Animated and viewWillDisappear:animated will be the best choice to do any thing with gestures as of my knowledge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.