2012-05-13 31 views
0

所以我通常遵循這種方式解僱ModalViewController當其觀點辭退ModalViewController與NavigationController在ipad當視野之外自來水

- (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 ..當我使用這種方式並點擊導航欄時,控制器被解散,或者當我從x控制器導航到「y」控制器並想要返回時,當我點擊後退按鈕ModalView被解僱!這對我來說是錯誤的行爲..我只是想要控制器在水龍頭完全在控制器外(視圖區域+ navigationBar區域)的情況下被解僱。任何人都可以提供幫助嗎?

回答

4

更新您的代碼如下:

- (void)handleTapBehind:(UITapGestureRecognizer *)sender 
{ 
    if (sender.state == UIGestureRecognizerStateEnded) 
    { 
     CGPoint flocation = [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. 

     CGPoint tap = [self.view convertPoint:flocation fromView:self.view.window]; 
     CGPoint tapBar = [self.navigationController.navigationBar convertPoint:flocation fromView:self.view.window]; 
     if (![self.view pointInside:tap withEvent:nil] && ![self.navigationController.navigationBar pointInside:tapBar withEvent:nil]) 
     { 
      // Remove the recognizer first so it's view.window is valid. 
      [self.view.window removeGestureRecognizer:sender]; 
      [self dismissModalViewControllerAnimated:YES]; 
     } 
    } 
} 

從窗口中刪除UITapGestureRecognizer當的UIBarButtonItem

自來水
-(void)viewWillDisappear:(BOOL)animated { 
    [self.view.window removeGestureRecognizer:self.tapOutsideGestureRecognizer]; 
}