gestureRecognizer shouldReceiveTouch persisting in deallocated view causing crash
Asked Answered
A

1

3

I have a fairly simple UITableView that pushes a new view on the stack. The new view has a gestureRecognizer that is initizalied like this

@synthesize swipeGestureLeft;


    - (void)viewDidLoad
{
        swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(toggleViewLeft)];
        swipeGestureLeft.numberOfTouchesRequired = 1;
        swipeGestureLeft.delegate=self;
        swipeGestureLeft.direction = (UISwipeGestureRecognizerDirectionLeft);
        [self.view addGestureRecognizer:swipeGestureLeft];
}

I also call the delegate method

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    if (viewShown==1) {
        return NO;
    }
    return YES;
}

and in the dealloc method I have

- (void)dealloc {
    NSLog(@"I AM IN DEALLOC");


    swipeGestureLeft.delegate=nil;
    [self.view removeGestureRecognizer:swipeGestureLeft];
    swipeGestureLeft=nil;



}

in my .h file I have

@interface MyViewController : UIViewController <UIGestureRecognizerDelegate>

now when I hit back to go back to my table view, the view gets deallocated (which I can see becasue the NSLog fires) now when I try and swipe down on my table view the app crashes with:

[MyViewController gestureRecognizer:shouldReceiveTouch:]: message sent to deallocated instance 

How do I ensure the delegate method is not called after the view has deallocated.

Alveta answered 13/9, 2013 at 1:1 Comment(4)
How are you declaring swipeGestureLeft?Gherkin
@property (strong,nonatomic) UISwipeGestureRecognizer *swipeGestureLeft;Alveta
Hmm. Not sure what's going on here. I dropped your code into the Master-Detail template but was unable to replicate the error.Gherkin
@Gherkin Thanks for the help. I solved it with the answer below.Alveta
A
7

SOLVED: I am using the new iOS7 navigationController.interactivePopGestureRecognizer and had set its delegate to "self" .

Upon deallocation I failed to set it's delegate to "nil". It was this object that was retaining (not sure if right word) the delegate call and not the swipeLeftGesture object. Setting its delegate to nil in the dealloc did the trick.

Alveta answered 13/9, 2013 at 2:55 Comment(1)
I tried setting the delegate to nil in dealloc but that seems to be too late. Had success with setting the delegate to nil in viewWillDisappear or overriding the UINavigationController and set the delegates correctly.Ransack

© 2022 - 2024 — McMap. All rights reserved.