iOS开发笔记

解决“whose view is not in the wind

2017-01-17  本文已影响664人  ship1912

在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];
上一篇 下一篇

猜你喜欢

热点阅读