whose view is not in the window hierarchy的解決辦法

場景:模態跳轉的時候,做提示框UIAlertController 寫在viewDidLoad中就會不出現提示框,且出現如下警告:

解決辦法:

你可以把這段代碼放入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];

這樣子就可以了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章