我建立了一种机制,可以通过点击视图的外部来关闭模态视图控制器。设置如下:
- (void)viewDidAppear:(BOOL)animated
{
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])
{
[self dismissModalViewControllerAnimated:YES];
NSLog(@"There are %d Gesture Recognizers",[self.view.window gestureRecognizers].count);
[self.view.window removeGestureRecognizer:sender];
}
}
}
这对于关闭单个模态视图非常有效。现在假设我有两个模态视图,一个从根视图控制器(视图 A)中调用,然后另一个模态视图从第一个模态(视图 B)中调用
有点像这样:
根视图 -> 视图 A -> 视图 B
当我点击关闭视图 B 时,一切都很好。但是,EXC_BAD_ACCESS
当我尝试关闭视图 A 时出现错误。打开僵尸后,视图 B 似乎仍在接收handleTapBehind:
发送给它的消息,即使在视图 B 关闭后它已被关闭并且内存不足。
我的问题是为什么视图 B 仍然收到消息?(handleTapBehind:
确保手势识别器应该已从关联的窗口中删除。)在视图 B 已经关闭后,如何将其发送到视图 A。
PS。上面的代码出现在 View A 和 View B 的控制器内部,并且是相同的。
编辑
这是我调用模态视图控制器的方式,此代码位于标准视图层次结构内的视图控制器内。
LBModalViewController *vc = [[LBModalViewController alloc] initWithNibName:@"LBModalViewController" bundle:nil];
[vc.myTableView setDataSource:vc];
[vc setDataArray:self.object.membersArray];
[vc setModalPresentationStyle:UIModalPresentationFormSheet];
[vc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[vc.view setClipsToBounds:NO];
[self presentViewController:vc animated:YES completion:nil];
// This is a hack to modify the size of the presented view controller
CGPoint modalOrigin = vc.view.superview.bounds.origin;
[[vc.view superview] setBounds:CGRectMake(modalOrigin.x, modalOrigin.y, 425, 351)];
[vc.view setBounds:CGRectMake(modalOrigin.x, modalOrigin.y, 425, 351)];
差不多就是这样,其他的都很标准。