从AppDelegate呈现UIAlertController
我想在我的iOS应用程序中从AppDelegate展示一个UIAlertController
。 以下是警报和现在的方法。
UIAlertController *alert = [UIAlertController alertControllerWithTitle:cTitle message:cMessage preferredStyle:UIAlertControllerStyleAlert]; //Configure alert and actions [self.window.rootViewController presentViewController:alert animated:TRUE completion:nil];
但是,当我尝试显示警报时,它不会显示,并在控制台中显示以下警报。
Warning: Attempt to present <UIAlertController: 0x145f5d60> on <UINavigationController: 0x146590f0> whose view is not in the window hierarchy!
什么是导致错误,我该如何解决?
如果你想从didFinishLaunchingWithOptions.Hope启动它,你也可以使用这个代码。
dispatch_async(dispatch_get_main_queue(), { let importantAlert: UIAlertController = UIAlertController(title: "Action Sheet", message: "Hello I was presented from appdelegate ;)", preferredStyle: .ActionSheet) self.window?.rootViewController?.presentViewController(importantAlert, animated: true, completion: nil) })
试试这个代码
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { NSLog(@"rakshapettu"); UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"AlertView" message:@"I am an AlertView" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [alert dismissViewControllerAnimated:YES completion:nil]; }]; [alert addAction:defaultAction]; UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; alertWindow.rootViewController = [[UIViewController alloc] init]; alertWindow.windowLevel = UIWindowLevelAlert + 1; [alertWindow makeKeyAndVisible]; [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil]; }
最好是使用像这样的东西:
var hostVC = self.window?.rootViewController while let next = hostVC?.presentedViewController { hostVC = next } hostVC?.presentViewController(alertController, animated: true, completion: nil)
如果您已经呈现模态视图控制器,则以前的答案将不起作用。
你可以在窗口打开之前调用它,并且实际显示navigationController:
“警告:试图提出谁的观点不在窗口层次!”
可能你在applicationDidFinishLaunching中这样做?
不pipe等等,当视图真的出现的时候,就像这样做
要么
一个'黑客'将会强制自己的观点和窗口:
[self.window addSubview:self.rootViewController.view]; [self.window makeKeyAndVisible];
我试图相同,但不起作用,因为viewcontroller的变化仍然返回初始viewcontroller并抛出whose view is not in the window hierarchy!
的错误whose view is not in the window hierarchy!
。 最后,我find了解决这个问题的办法:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
但我必须强制视图控制器来更新de keyWindow
:
override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) UIApplication.sharedApplication().keyWindow!.rootViewController = self UIApplication.sharedApplication().keyWindow!.makeKeyAndVisible() }
你可以尝试在viewDidAppear
调用它,而不是viewDidLoad
。 我有一个类似的错误,并修复它。
使用警报控制器:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { dispatch_async(dispatch_get_main_queue(), { //INTERNET NOT AVAILABLE ALERT var internetUnavailableAlertController = UIAlertController (title: "Network Unavailable", message: "Please check your internet connection settings and turn on Network Connection", preferredStyle: .Alert) var settingsAction = UIAlertAction(title: "Settings", style: .Default) { (_) -> Void in let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString) if let url = settingsUrl { UIApplication.sharedApplication().openURL(url) } } var cancelAction = UIAlertAction(title: "Okay", style: .Default, handler: nil) internetUnavailableAlertController .addAction(settingsAction) internetUnavailableAlertController .addAction(cancelAction) self.window?.rootViewController!.presentViewController(internetUnavailableAlertController , animated: true, completion: nil) })
使用AlertView:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { dispatch_async(dispatch_get_main_queue(), { //INTERNET NOT AVAILABLE ALERTVIEW let internetUnavailableAlert = UIAlertView(title: "Network Unavailable", message: "Please check your internet connection settings and turn on Network Connection", delegate: self, cancelButtonTitle: "Okay") internetUnavailableAlert.show() }) return true } }
- iPhone registerForRemoteNotificationTypes不会产生错误,但不会触发授予设备令牌的委托
- applicationWillEnterForeground提供一个视图控制器
- 其视图不在窗口层次结构中 – 电子邮件表单
- NSInternalInconsistencyException',原因:'无法在捆绑中加载NIB:'NSBundle
- 在Swift中从AppDelegate获取ViewController的实例
- 简单的Webview:navigationDelegate开始崩溃
- 如何在没有用户交互的情况下在后台接收iOS通知
- iOS背景获取和完成块
- Swift – 在App Delegate中实例化没有故事板的导航控制器