我有一个针对 iPad 的视图,我通过首先将其视图控制器推入导航控制器,以模态方式将其modalPresentationStyle
属性设置为UIModalPresentationFormSheet
:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
然后,在呈现的视图控制器中,我想检测自身外部的点击手势(正如我所说,我将其呈现为表单),所以我以这种方式设置了点击手势:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
self.tapGestureRecognizer.numberOfTouchesRequired = 1;
self.tapGestureRecognizer.numberOfTapsRequired = 1;
self.tapGestureRecognizer.cancelsTouchesInView = NO;
[self.view.window addGestureRecognizer:self.tapGestureRecognizer];
}
- (void)handleTap:(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.view.window removeGestureRecognizer:sender];
[self dismissModalViewControllerAnimated:YES];
}
}
}
我以模态方式呈现的导航控制器,其根视图控制器是设置此手势识别器的控制器,它在层次结构中显示更多视图。当仅将根视图控制器推送到导航控制器堆栈上并且我在其外部使用点击手势时,它会被正确解除,并且我能够再次以模态方式呈现此根视图控制器。当我从根视图控制器导航并将另一个视图控制器推送到导航堆栈时,点击手势仍然有效,但是当我尝试再次显示表单时应用程序崩溃。
我应该如何处理这个手势和我想要有导航层次结构的行为?
提前致谢