0

我有點難住在這裏。我有一個UIModalPresentationFormSheet,並且我添加了一個手勢識別器來處理解僱表單,如果用戶選擇表單外的任何地方。我還在表單頂部的導航欄中有一個取消按鈕。當用戶選擇表單外的任何地方以使用手勢識別器來解除表單時,一切正常。但是,當他們使用取消按鈕時,忽略手勢識別器,一旦表單被關閉,我會得到下面的錯誤。我相信它從識別器被髮送到handleTapBehind方法。我不明白爲什麼,因爲當視圖被解僱時,不應該調用viewWillAppear,它將識別器分配給釋放的方法(handleTapBehind)。iOS消息發送到解除分配的實例

錯誤:

[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]; 
    } 
} 
} 

回答

1

以下內容添加到您的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).

尤其是「被分配識別器解除分配的方法」?

+0

不,它沒有工作。 –

+0

你檢查了viewWillDisappear是否被調用? –

+0

是的,它被調用。 –

相關問題