簡體   English   中英

UIAlertView取消按鈕使應用程序崩潰

[英]UIAlertView cancel button crashes app

好的,所以我才剛剛開始iOS開發。 我將從解釋我的應用程序流程開始:
1.加載了一個名為“ appViewController”的視圖。
2. [self presentViewController: controller animated: YES completion:nil]; 這會加載一個webview
3.完成webview后,我將其關閉並以這種方式加載新的UINavigation:

[self dismissViewControllerAnimated:YES completion:^{
        formViewController *fv = [ [formViewController alloc] init ];
        UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:fv] autorelease];
        navController.navigationBar.hidden = YES;
        [presentingViewController presentModalViewController:navController animated:YES];

    }];

5. formViewController有一個按鈕,該按鈕具有附加的事件,該事件以這種方式顯示警報

UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Oops!"
                                           message:@"test"
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
[av show];

到現在為止,一切都按預期進行。 現在,當我單擊“確定”(取消)按鈕時,應用程序崩潰,並顯示NSZombieEnabled
-[appViewController _deepestDefaultFirstResponder]:消息發送到已釋放實例0x6e6a570 lldb

這是怎么回事 為什么嘗試再次將消息發送到appViewController? [av show]之后沒有代碼

注意:我正在使用ARC

如果您使用的是arc,則代碼中的自動發布無效。

看來您的根視圖控制器在某個時候被釋放了,並且當響應者鏈被遍歷時,生成的懸空指針就被訪問了。

為了驗證這一點,我將在appViewController上實現dealloc方法,看看它是否被調用。

dealloc {
  NSLog(@"Problems ahead.");
}

如果在您希望發生這種情況之前確實調用了該方法(對於根視圖控制器可能根本沒有),則需要找出發生這種情況的原因。 您可能在某處缺少強大的參考。 檢查您的應用程序委托,並確認您對該窗口有很好的引用,並且將您的控制器設置為根視圖控制器(前提是您未使用情節提要)。

僵屍儀器非常適合調試此類問題。 它將列出您有問題的對象的所有保留和發布。 這是它的簡短介紹

有幾件事。

  1. 您正在混合presentViewController和presentModalViewController。 如果您不熟悉iOS,則應始終使用presentViewController。 無需習慣使用即將棄用的方法(請參見此問題的答案presentModalViewController和presentViewController之間的區別?

  2. 通常,除非絕對必要,否則您不應自動釋放。 由於向控制器提供presentViewController(和presentModalViewController)時,將保留該控制器,因此以后可以輕松釋放它。 我將這樣重組:

 UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:fv]; navController.navigationBar.hidden = YES; [presentingViewController presentModalViewController:navController animated:YES]; [navController release]; 

3 ..您包括的代碼部分在哪里? 就像,您按一個按鈕,視圖就消失了還是什么?

暫無
暫無

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

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