我有点被难住了。我有一个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/