0

I have a UITapGestureRecognizer (TapGes) on the UIWindow with target on a UIViewController (vc).

I don't and I can't have a strong pointer to TapGes from vc. So after certain time vc is going to dealloc (when its done) and at this point I would like to remove TapGes from the UIWindow but I haven't found a way to do so.

If it was a view I would do something like:

  1. Set a Tag to that view
  2. Iterate on UIWindow subviews
  3. Find the UIView with that tag
  4. Remove it from superview

The problem is that UITapGestureRecognizer does not have a way of identify it like tag.

Is there a solution to this problem with the conditions:

  • Not having a strong pointer to UITapGestureRecognizer
  • Not adding an aux view on to UIWindow to add the UITapGestureRecognizer to it and then remove this view.

Thank you.

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
Franklin
  • 881
  • 1
  • 8
  • 28
  • Override the vc's deallocate method and deal with the UITapGestureRecognizer? – Danyun Liu Sep 04 '14 at 03:17
  • I cant. In that method I would like to remove the UITapGestureRecognizer but I dont have a strong pointer to it, so I cant do [self.view.window removeGestureRecognizer: ]... – Franklin Sep 04 '14 at 03:21
  • You can just use a weak property to handle the recognizer,@property (weak, nonatomic) UITapGestureRecognizer *tap in your vc. – Danyun Liu Sep 04 '14 at 03:24

1 Answers1

1

You can enumerate all the gestures of UIWindow when your viewController is deallocated and compare the target of each of them with self , then remove the UITapGestureRecognizer.

How to get the target of UITapGestureRecognizer, there are two solutions:

First solution: Subclass UITapGestureRecognizer, add a weak property named myTarget to keep the target and override the method - (void)addTarget:(id)target action:(SEL)action

- (void)addTarget:(id)target action:(SEL)action
{
    [super addTarget:target action:action] ;
    _myTarget = target ;
}

If you use first solution, you should check if the gesture recognizer is kind of your subclass using isKindOfClass:.

Second solution :Referenced from here

NSMutableArray *targets = [myGes valueForKeyPath:@"_targets"];
id targetContainer = targets[0];//get first target for example
id targetOfMyGes = [targetContainer valueForKeyPath:@"_target"];
NSLog(@"%@", targetOfMyGes );//you can see reference for target object
Community
  • 1
  • 1
KudoCC
  • 6,912
  • 1
  • 24
  • 53