因此,当在其视图之外点击时,我通常会按照这种方式关闭 ModalViewController
- (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; //So the user can still interact with controls in the modal view
[self.view.window addGestureRecognizer:recognizer];
[recognizer release];
}
}
}
- (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];
[self.view.window removeGestureRecognizer:recognizer];
}
}
}
它与常规的 ModalViewController 一起工作得很好 .. 但现在我有一个 modelViewController ,它是一个以“x”viewController 作为根的 NavigationViewController ..当我使用这种方式并点击导航栏时,控制器被关闭,或者当我导航到“ y" 控制器来自 x 控制器并想要返回,当我单击后退按钮时,ModalView 也会被关闭!这对我来说是错误的行为..我只是希望在点击完全在控制器之外(视图区域+导航栏区域)的情况下关闭控制器..任何人都可以提供帮助吗?