欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

whose view is not in the window hierarchy的解决办法

程序员文章站 2022-06-17 19:47:28
...

场景:模态跳转的时候,做提示框UIAlertController 写在viewDidLoad中就会不出现提示框,且出现如下警告:

whose view is not in the window hierarchy的解决办法

解决办法:

你可以把这段代码放入viewDidAppear里边,但是我觉得你获取到当前控制器更好:

 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"sdsdfsdf" preferredStyle:  UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
        UIViewController *topRootViewController = [[UIApplication  sharedApplication] keyWindow].rootViewController;
        
        // 循环
        while (topRootViewController.presentedViewController)
        {
            
            topRootViewController = topRootViewController.presentedViewController;
        }
        // 然后再进行present操作
        [topRootViewController presentViewController:alert animated:true completion:nil];

这样子就可以了

转载于:https://my.oschina.net/rainwz/blog/2243751