问题描述
项目中,点击“退出登录”按钮,弹出alertView提示用户“确定退出登录吗?”,点击alertView上的确定按钮后,设置keyWindow为LoginController。
代码如下:
- (void)logOut {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"确定要退出吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
#pragma mark alertView代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
//跳转到登录界面
LoginController *loginVC = [[LoginController alloc] init];
[UIApplication sharedApplication].keyWindow.rootViewController = loginNewVC;
}
}
运行过后,Xcode给出如下警告:
attempt to dismiss modal view controller whose view does not currently appear. self = <_UIModalItemAppViewController: 0x15f5fb10> modalViewController = <_UIModalItemsPresentingViewController: 0x15f62ab0>
问题解决
通过分析,问题应该是出在alertView还未完全dismiss掉就马上切换了根视图控制器。(alertView默认dismiss时带动画效果,需要消耗一定时间)。
所以,这里使用alertView的另一个代理方法解决问题,这个方法在alertView完全dismiss掉后调用,如下:
//This method is invoked after the animation ends and the view is hidden.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex