Ios UITapGestureRecognitor在解除viewController后导致崩溃

Ios UITapGestureRecognitor在解除viewController后导致崩溃,ios,objective-c,ipad,Ios,Objective C,Ipad,正在尝试将UITapGestureRecognitor实现到表单工作表模态viewcontroller。 若用户触摸表单表的外部,表单表应该关闭,这样代码就可以正常工作 问题是,如果我手动关闭表单工作表,并尝试触摸视图中的任何点,它仍会尝试调用UITapGestureRecognizer方法,并且应用程序崩溃 Error :: -[xxxxView handleTapBehind:]: message sent to deallocated instance -(void)done

正在尝试将UITapGestureRecognitor实现到表单工作表模态viewcontroller。 若用户触摸表单表的外部,表单表应该关闭,这样代码就可以正常工作

问题是,如果我手动关闭表单工作表,并尝试触摸视图中的任何点,它仍会尝试调用UITapGestureRecognizer方法,并且应用程序崩溃

Error ::
    -[xxxxView handleTapBehind:]: message sent to deallocated instance



-(void)done
{
    [self.navigationController popViewControllerAnimated:YES];
        //send notification that folder has been created
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshDetails" object:nil];
}
-(void)viewDidAppear:(BOOL)animated
{
    // gesture recognizer to cancel screen
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
    [recognizer setNumberOfTapsRequired:1];
    recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
    [self.view.window addGestureRecognizer:recognizer];

}

- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window

        //Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.

        if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil])
        {
            // Remove the recognizer first so it's view.window is valid.
            [self.view.window removeGestureRecognizer:sender];
            [self dismissModalViewControllerAnimated:YES];
        }
    }
}

为什么handleTapBehind:在我关闭viewcontroller后仍然调用?我怎样才能解决这个问题

将手势识别器添加到窗口:

[self.view.window addGestureRecognizer:recognizer];
并为你的控制器设定目标

所以,当你的控制器关闭时-它被释放,但手势识别器仍然活着。当它启动时,它会尝试向您的控制器发送操作,而您的控制器已经不存在


所以,您应该将识别器添加到控制器的视图中,或者在ViewWilldisPaper方法中将其删除。

您将手势识别器添加到窗口:

[self.view.window addGestureRecognizer:recognizer];
并为你的控制器设定目标

所以,当你的控制器关闭时-它被释放,但手势识别器仍然活着。当它启动时,它会尝试向您的控制器发送操作,而您的控制器已经不存在


因此,您应该在控制器的视图中添加识别器,或在ViewWillDisPaper方法中删除识别器。

尝试使用手势识别器的委托方法,在手动解除vc时放置一个标志,并在shouldReceiveTouch中:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
   if(flag)
   {
      return NO;
   }
   return YES;
}

不要忘记将代理设置为当前viewcontroller。您现在还可以删除行[self.view.window removeGestureRecognizer:sender]

尝试使用手势识别器的委托方法,手动解除vc并在shouldReceiveTouch中放置一个标志:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
   if(flag)
   {
      return NO;
   }
   return YES;
}

不要忘记将代理设置为当前viewcontroller。您现在还可以删除行[self.view.window removeGestureRecognizer:sender]

我想添加@mikhail答案中的代码

 (UITapGestureRecognizer *)senderTap

-(void)viewWillDisappear:(BOOL)animated{

     [self.view.window removeGestureRecognizer:senderTap];
}

在我的代码中,当调用ViewDidAppear时,我在选择器方法中捕获UITapGestureRecognitizer sender。

我想添加@mikhail答案中的代码

 (UITapGestureRecognizer *)senderTap

-(void)viewWillDisappear:(BOOL)animated{

     [self.view.window removeGestureRecognizer:senderTap];
}

在我的代码中,当调用ViewDidAppear时,我用选择器方法捕获UITapgestureRecognitor发送者。

@SpaceDust同意。将手势添加到self.view,而不是self.view.window。@SpaceDust同意。将手势添加到self.view,而不是self.view.window。