whose view is not in the window

2018-07-21  本文已影响15人  LD_左岸

whose view is not in the window hierarchy!

Warning: Attempt to present <UIAlertController: 0x7fc5e9048c00> on <ViewController: 0x7fc5e7e0d9e0> 
whose view is not in the window hierarchy!

这个警告是

[self.window makeKeyAndVisible];
之前拿
self.window.rootViewController presentViewController:alertVc animated:YES completion:nil];
导致的
也是这三个⚠️里解决最容易的一个

which is already presenting

这个警告是

我在didFinishLaunchingWithOptions里进行了
self.window.rootViewController presentViewController:alertVc animated:YES completion:nil];
又在applicationDidBecomeActive里进行了
self.window.rootViewController presentViewController:alertVc animated:YES completion:nil];
相当于间隔了1s?进行了两次self.window.rootViewController的present操作 
显示这并不是合适的

于是将代码修改为

 UIWindow *fK = [[UIApplication sharedApplication] keyWindow];
        //如果window已有弹出的视图,会导致界面无法弹出,页面卡死,这里需要先把视图关闭,再弹出
        if (fK.rootViewController.presentedViewController != nil) {
            [fK.rootViewController.presentedViewController dismissViewControllerAnimated:NO completion:nil];
           }
            [fK.rootViewController presentViewController:alertVc animated:YES completion:nil];
       }

如上改完后_报了第三个警告⚠️也就是

while a presentation is in progress!

考虑到有可能是dismiss没执行完所以最终把代码改成这 总算没⚠️了

UIWindow *fK = [[UIApplication sharedApplication] keyWindow];
        //如果window已有弹出的视图,会导致界面无法弹出,页面卡死,这里需要先把视图关闭,再弹出
        if (fK.rootViewController.presentedViewController != nil) {
            [fK.rootViewController.presentedViewController dismissViewControllerAnimated:NO completion:^{
                
                [fK.rootViewController presentViewController:alertVc animated:YES completion:nil];
                
            }];
            
        }else{
            [fK.rootViewController presentViewController:alertVc animated:YES completion:nil];
        }
        
      }

鉴于能力有限 水平一般 理解有误之处 深望不吝指出 阿门!

上一篇 下一篇

猜你喜欢

热点阅读