iOS開發報錯之attempt to dismiss modal view controller whose view does not currently appear
剛才遇到一個問題,現在在這就當紀錄一下,大家有遇到的能快速找到原因,分享一下啊。 在APP中,需要使用者登入後才能使用,所以我通過更改APP的[UIApplicationsharedApplication].keyWindow.rootViewController來控制介面的跳轉。 在使用過程中出現如下問題: 1.登入成功後點擊登出按鈕,彈出登出提示框UIAlertView; 2.登出成功後重新登入; 3.再次點擊登出不再彈出UIAlertView。 提示如下警告: 點擊登出按鈕執行更改rootvie操作: attempt to dismiss modal view controller whose view does not currently appear 再次點擊登出的時候提示: Attempt to present <_UIModalItemsPresentingViewController: 0x7f9d1b5b2fd0> on <_UIModalItemAppViewController: 0x7f9d1d335520> whose view isnot in the window hierarchy! My Code是: LoginViewController *loginVC = [[LoginViewControlleralloc] init]; CNavigationController *nav = [[CNavigationControlleralloc] initWithRootViewController:loginVC]; [UIApplicationsharedApplication].keyWindow.rootViewController = nav; 造成這個的原因主要是: 因為我執行上述代碼是在:-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex的這個方法執行的; 所以在我執行切換根視圖控制器的時候UIAlertView是還沒有消失的,所以會出現上述錯誤,UIAlertView的消失是需要一定的時間的, 解決方案: 要解決這個問題,就是在UIAlertView的另一個代理方法-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex中執行切換根 控制器的操作,即上述My Code,也就是說在UIAlertView徹底消失後再執行切換根控制器,解決!