iOS 消息发送到已释放的实例

标签 ios uigesturerecognizer modalviewcontroller viewdidappear

我有点被难住了。我有一个UIModalPresentationFormSheet我添加了一个手势识别器来处理在用户选择表单之外的任何位置时关闭表单的情况。我在表单顶部的导航栏中还有一个取消按钮。当用户选择表单之外的任何位置以使用手势识别器关闭表单时,一切正常。但是,当他们使用取消按钮时,忽略手势识别器,一旦表单关闭,我就会收到以下错误。我相信它是从识别器发送到handleTapBehind 方法的。我不明白为什么,因为当 View 被忽略时,viewWillAppear不应调用将识别器分配给已解除分配的方法 ( handleTapBehind )。

错误:

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

代码:

- (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];
    }
}
}

最佳答案

将以下内容添加到 viewController 的 viewWillDisappear 中:

recognizer.delegate=nil;

希望这对您有所帮助。

PS:我不明白你的最后一句话:

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).

特别是“正在将识别器分配给已释放的方法”?

关于iOS 消息发送到已释放的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12991472/

相关文章:

iphone - 如何在 Storyboard中创建 UIViewController 布局,然后在代码中使用它?

ios - 在 iPad 中使用 NavigationController 关闭 ModalViewController 当在 View 外点击时

ios - 避免在 Objective-C 中滥用单例

ios - 手势关闭键盘后,TableView 点击失败

ios - 创建每个用户唯一的密码,其他用户无法使用

ios - UILongPressGestureRecognizer 和 UIPanGestureRecognizer 组合

ios - 添加手势识别器以允许在 Swift 中的 MapView 上水平滑动

ios - 将 UISearchBar returnKeyType 更改为 Done 在 iPhone 5 上有效,但在 iPhone 4s 上会使应用程序崩溃

objective-c - UIScrollView 的 subview 中的水平 UISwipeGestureRecognizer? (UIScrollView 只需要识别垂直滚动)

iphone - 如何制作仅覆盖一半屏幕的模态日期选择器?