2013-04-09 41 views
1

我正在使用UIWebView,並且它在點擊按鈕(從javascript)時彈出警告。還有另一個按鈕(在本機方面),它關閉了控制器,所以也分配了UIWebView。殭屍UIWebView,當它剛剛從javascript調用alert()後釋放時分配

問題是,如果我觸摸UIWebView中的按鈕,並在提示填充之前觸摸關閉按鈕,我的控制器和UIWebView將被釋放,但警報仍會保留在屏幕上。然後,如果我點擊警報,應用程序崩潰的任何按鈕,並給出以下錯誤:

[UIWebView modalView:didDismissWithButtonIndex:]: message sent to deallocated instance 

這種方法是從私有方法

[UIModalView(Private) _popoutAnimationDidStop:finished:] 

叫我使用ARC,和我的dealloc是這樣的:

- (void)dealloc { 
    [_myWebView stopLoading]; 
    _myWebView.delegate = nil; 
    _myWebView = nil; 
} 

但是,這並沒有解決我的問題,因爲我覺得UIModalView有我的WebView爲代表的參考,我不能將其設置爲無,因爲它的私密性。

我該如何解決?

問候

回答

1

想辦法解除分配的UIWebView之前設置爲零的UIAlertView中的代表。

+0

我無法訪問UIAlertView,因爲它是由UIWebView私下處理的。 – manuyavuz 2013-04-09 11:45:41

+0

您可以掛鉤UIAlertView委託功能。所以你可以保留UIWebView以防止它太早發佈。 – 2017-06-21 06:20:24

1

這是處理警報視圖時發生的Apple錯誤。 Open a bug report.

在此期間,這裏有一些解決方法:

創建的UIAlertView中類別:

@interface UIAlertView (QuickDismiss) @end 

@implementation UIAlertView (QuickDismiss) 


- (void)__quickDismiss 
{ 
    [self dismissWithClickedButtonIndex:self.cancelButtonIndex animated:NO]; 
} 

@end 

現在,在您的視圖控制器的dealloc方法,稱之爲:

[[UIApplication sharedApplication] sendAction:@selector(__quickDismiss) to:nil from:nil forEvent:nil]; 

這將關閉當前打開的所有警報視圖,包括Web視圖顯示的視圖。

如果這不起作用,您可以始終遍歷所有UIApplication.sharedApplication.windows對象的所有子視圖,檢查[view.class.description hasPrefix:@"UIAlertView"]是否爲真,並解散該對象。這是一個不如以前的方法,應該是最後的手段。

祝你好運。

0

最後,我找到了一個很好的解決方案,它的工作原理。我使用的方法調酒掛鉤UIAlertView中委託功能 - (空)didPresentAlertView:(UIAlertView中*)alertView

- (void)didPresentAlertView:(UIAlertView *)alertView 
{ 
    if ([self.delegate respondsToSelector:@selector(didPresentAlertView:)]) { 
     [self.delegate didPresentAlertView:alertView]; 
    } 
    if ([self.delegate isKindOfClass: [UIWebView class]]) { 
     uiWebView = self.delegate; 
    } 
} 

我只是保留UIWebView實例在此功能使得UIWebView實例作爲UIAlertView中的代表將不UIAlertView中實例之前發佈被釋放。

相關問題