在iOS 中弹窗时有时会失败,打印下面的错误信息
Warning: Attempt to present on whose view is not in the window hierarchy!
调用方法是
[self presentViewController:secondView animated:YES completion:nil];
原因是因为self有可能不是顶层窗口,改用下面的方式调用
UIViewController * top= [UIApplicationsharedApplication].keyWindow.rootViewController;
[top presentViewController:secondView animated:YES completion:nil];
用上面的代码有时还是不能解决,最终改用下面的代码
-(UIViewController *) topMostController {
UIViewController*topController = [UIApplicationsharedApplication].keyWindow.rootViewController;
while(topController.presentedViewController){
topController=topController.presentedViewController;
}
returntopController;
}
UIViewController * top = [self topMostController];
[top presentViewController:secondView animated:YES completion:nil];
网友评论