0

I am kinda stumped here. I have a UIModalPresentationFormSheet and I have added a gesture recognizer to handle dismissing the form sheet if the user selects anywhere outside the form sheet. I also have a cancel button in the navbar at the top of the form sheet. When the user selects anywhere outside the formsheet to use the gesture recognizer to dismiss the form sheet, everything works fine. But when they use the cancel button, ignoring the gesture recognizer, once the form sheet is closed I get the error below. I believe its from recognizer being sent to the handleTapBehind method. I dont understand why though because when the view is dismissed, the viewWillAppear should not be called which is allocating the recognizer to a deallocated method (handleTapBehind).

Error:

[CallWebViewViewController handleTapBehind:]: message sent to deallocated instance 0x21ee5db0

Code:

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

if(UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM())
{
    if(![self.view.window.gestureRecognizers containsObject:recognizer])
    {
        recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];

        [recognizer setNumberOfTapsRequired:1];
        recognizer.cancelsTouchesInView = NO;
        [self.view.window addGestureRecognizer:recognizer];
    }
}
}

- (void)handleTapBehind:(UITapGestureRecognizer *)sender {

if (sender.state == UIGestureRecognizerStateEnded)
{
    CGPoint location = [sender locationInView:nil];

    if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil])
    {
        [self dismissViewControllerAnimated:YES completion:nil];
        [self.view.window removeGestureRecognizer:recognizer];
    }
}
}
atxe
  • 5,029
  • 2
  • 36
  • 50
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73

1 Answers1

1

Add the following to the viewWillDisappear of your viewController:

recognizer.delegate=nil;

Hope this helps.

PS: I don't understand your last sentence:

I dont understand why though because when the view is dismissed, the viewWillAppear should not be called which is allocating the recognizer to a deallocated method (handleTapBehind).

Especially "is allocating the recognizer to a deallocated method" ?

FD_
  • 12,947
  • 4
  • 35
  • 62