美文网首页iOS开发笔记
解决“whose view is not in the wind

解决“whose view is not in the wind

作者: ship1912 | 来源:发表于2017-01-17 10:49 被阅读664次

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

    相关文章

      网友评论

      本文标题:解决“whose view is not in the wind

      本文链接:https://www.haomeiwen.com/subject/ljdmbttx.html