在iOS 中彈窗時(shí)有時(shí)會(huì)失敗隅要,打印下面的錯(cuò)誤信息
Warning: Attempt to present on whose view is not in the window hierarchy!
調(diào)用方法是
[self presentViewController:secondView animated:YES completion:nil];
原因是因?yàn)閟elf有可能不是頂層窗口捧弃,改用下面的方式調(diào)用
UIViewController * top= [UIApplicationsharedApplication].keyWindow.rootViewController;
[top presentViewController:secondView animated:YES completion:nil];
用上面的代碼有時(shí)還是不能解決隐砸,最終改用下面的代碼
-(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];