簡體   English   中英

在iOS中單擊警報時應用崩潰

[英]app crashes when clicks on alert in ios

以下是在視圖控制器中顯示警報的代碼

-(void)saveProducts {
    pData = [[JsonModel sharedJsonModel] prodData];
    if ([pData count] == 0 && [self respondsToSelector:@selector(alertView:clickedButtonAtIndex:) ]  ) {
        alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"No products against this category" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }


    [self.tblView reloadData];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
        [actInd stopAnimating];
    }

}

但是,在網絡速度較慢的情況下,警報會緩慢發出。 如果同時單擊導航欄的后退按鈕,則彈出導航控制器並在新的視圖控制器中顯示警報。 但是,當我單擊“確定”時,應用程序突然崩潰,並顯示EXC_BAD_ACCESS錯誤。 我也試過

didDismissWithButtonIndex

功能代替

clickedButtonAtIndex

但是發生同樣的錯誤。 請幫我

如果我們不單擊后退按鈕,它將正常工作。 僅當第一視圖控制器警報顯示在第二視圖控制器中時,才會出現問題

編輯這是錯誤報告* -[ProductsListing alertView:didDismissWithButtonIndex:]:消息發送到已釋放實例0x8478280

編輯我了解這個問題。 當我單擊“后退”按鈕時,我的警報委托解除分配並委托呼叫結果錯誤。 我該如何克服?

我最好的猜測是'self.navigationController'或'actInd'已經發布。 另外,“ UIAlertView”會泄漏內存(除非您使用的是ARC)。 使用樂器分析應用程序的輪廓,選擇“僵屍”工具,然后查看其附帶的內容。

根據您所描述的,這里的問題可能是這樣(一個瘋狂的猜測)

[actInd stopAnimating];

在viewController移除(彈出)之后調用actInd可能沒有有效的內存,因此崩潰

像這樣更改方法內容並檢查

if (buttonIndex == 0) {
        [actInd stopAnimating];
        [self.navigationController popViewControllerAnimated:YES];
    }

快樂的編碼:)

我相信你必須改變

[alert show];

if(self.view.window){
   [alert show];
}

這樣,僅當控制器(視圖)仍在屏幕上時才顯示警報。(為什么要讓用戶從上一個屏幕中看到警報?)如果您仍然希望顯示警報,則...“舊”控制器必須通知“新”用戶發生了問題...現在是新控制器的工作來通知用戶。

或者您可以嘗試更改此部分

    [self.navigationController popViewControllerAnimated:YES];
    [actInd stopAnimating];  

if(self.view.window){
    [self.navigationController popViewControllerAnimated:YES];
    [actInd stopAnimating]; // im not sure where the animation is...so not sure if this shoulb be in here or not
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM