尝试在视图不在窗口层次结构中的UIViewController上呈现UIAlertController [英] Attempt to present UIAlertController on UIViewController whose view is not in the window hierarchy
问题描述
我正在尝试为示例应用程序检查iOS 9中的UIAlertController,然后在运行它时发现控制台中出现警告.我正在使用Xcode 7和ObjectiveC.
I'm trying to check the UIAlertController in iOS 9 for my sample application and while run it then I had found warning in console. I'm using Xcode 7 and Objective C.
请在控制台中找到以下警告消息.
Please find the below warning message in console.
警告:尝试提出< UIAlertController:0x7fb1bb5be040>开启 < ViewController:0x7fb1bb5aef30>其视图不在窗口中 等级!
Warning: Attempt to present < UIAlertController: 0x7fb1bb5be040 > on < ViewController: 0x7fb1bb5aef30 > whose view is not in the window hierarchy!
请找到以下代码以获取更多信息.
Please find the below code for more information.
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
message:@"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
推荐答案
我想,您正在尝试在加载后的视图上显示警报.您收到错误:
I guess, you are trying to present the alert on view did load. You get the error:
警告:尝试提出< UIAlertController:0x7fb1bb5be040>在<上 ViewController:0x7fb1bb5aef30>其视图不在窗口中 等级!
Warning: Attempt to present < UIAlertController: 0x7fb1bb5be040 > on < ViewController: 0x7fb1bb5aef30 > whose view is not in the window hierarchy!
因为在加载视图后,视图仍无法显示给用户.因此,您无法显示警报.将代码移到viewDidAppear
because, in view did load the views are not yet available to display to the user. Hence you can't present the alert. Move the code into viewDidAppear
这篇关于尝试在视图不在窗口层次结构中的UIViewController上呈现UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!