在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];