Present UIAlertController from AppDelegate
来源:互联网 发布:学java最好的学 编辑:程序博客网 时间:2024/06/05 05:00
转自:http://stackoverflow.com/questions/26952061/present-uialertcontroller-from-appdelegate
I'm trying to present a UIAlertController
from the AppDelegate in my iOS app. Below is the alert and the present method.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:cTitle message:cMessage preferredStyle:UIAlertControllerStyleAlert];//Configure alert and actions[self.window.rootViewController presentViewController:alert animated:TRUE completion:nil];
However, when I try to present the alert, it doesn't appear and I get the following alert in the console.
Warning: Attempt to present <UIAlertController: 0x145f5d60> on <UINavigationController: 0x146590f0> whose view is not in the window hierarchy!
What is causing the error and how do I fix it?
8 Answers
You can use this code as well if you want to launch it from didFinishLaunchingWithOptions.Hope this helps.
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) })
UIAlertController
needs a pointer to the current UIViewController
. – Zeb Sep 16 '15 at 12:22you call it before the window is up and the navigationController is actually shown:
"Warning: Attempt to present on whose view is not in the window hierarchy!"
likely you do so in applicationDidFinishLaunching?
EITHER wait .. like do it when the view really appears
OR
one 'hack' would be to force the view and window up yourself:
[self.window addSubview:self.rootViewController.view];[self.window makeKeyAndVisible];
try this code..
-(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]; }
It's better to use something like:
var hostVC = self.window?.rootViewControllerwhile let next = hostVC?.presentedViewController { hostVC = next}hostVC?.presentViewController(alertController, animated: true, completion: nil)
Previous answers won't work if you are already presenting modal view controller.
I was trying the same but does not work because after change of viewcontroller still returned the initial viewcontroller and throw the error whose view is not in the window hierarchy!
. Finally, I found the solution to this problem:
UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
But I have to force view controllers to update de keyWindow
with this:
override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) UIApplication.sharedApplication().keyWindow!.rootViewController = self UIApplication.sharedApplication().keyWindow!.makeKeyAndVisible()}
You can try calling it in viewDidAppear
instead of viewDidLoad
. I had a similar error and that fixed it.
Using Alert Controller:
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) })
Using 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 }}
You are trying to present UIAlertController on UINavigationController, which appeared to be your rootViewController and the problem is you cannot present UIAlertController on UINavigationController, the UIAlertController needs a view controller's from which it can be presented.
Can you not call as follow,
[self presentViewController:alert animated:YES completion:nil];
here self refers to current view controller, on which you are presenting the alert.
- Present UIAlertController from AppDelegate
- Swift-在AppDelegate弹框(UIAlertController)进行present跳转--学习笔记
- B. Present from Lena
- Present from Lena
- 使用AlertController出现:Warning: Attempt to present <UIAlertController: 0x7a961600>
- "CreateRSADataVerifier(NSString*)", referenced from:,[AppDelegate parseURL:application:]
- AppDelegate
- AppDelegate
- AppDelegate
- AppDelegate
- AppDelegate
- AppDelegate
- AppDelegate
- AppDelegate
- AppDelegate
- AppDelegate
- AppDelegate
- appdelegate
- activemq主从和集群结合的部署方式
- 通过微信二维码分享自己的软件
- 网络请求发生错误: Unsupported Media Type
- lua中io操作的解释
- Selenium WebDriver高级用法 - 选择合适的WebDrvier
- Present UIAlertController from AppDelegate
- matlab和python中引用libsvm工具包
- C++运算符重载
- 搜索框
- Caffe的安装与测试
- unity3D游戏开发十三之GUI
- 知识点总结
- javascript继承方法
- 随机森林,GBDT,Adaboost原理及python实现