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.