从AppDelegate展示UIAlertController

时间:2014-11-15 23:37:39

标签: ios objective-c 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!

导致错误的原因是什么?如何解决?

7 个答案:

答案 0 :(得分:28)

如果您想从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)
        })

答案 1 :(得分:18)

试试这段代码..

-(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]; 
}

答案 2 :(得分:12)

最好使用以下内容:

var hostVC = self.window?.rootViewController

while let next = hostVC?.presentedViewController {
    hostVC = next
}

hostVC?.presentViewController(alertController, animated: true, completion: nil)

如果您已经提出模态视图控制器,之前的答案将无法使用。

答案 3 :(得分:5)

在窗口启动之前调用它并且实际显示了navigationController:

&#34;警告:尝试显示其视图不在窗口层次结构中!&#34;

你可能在applicationDidFinishLaunching中这样做了吗?


等待..就像在视图真正出现时一样

OR

一个&#39; hack&#39;将自己强迫视图和窗口:

[self.window addSubview:self.rootViewController.view];
[self.window makeKeyAndVisible];

答案 4 :(得分:2)

我尝试了相同但不起作用,因为更改viewcontroller后仍然返回初始viewcontroller并抛出错误whose view is not in the window hierarchy!。最后,我找到了解决此问题的方法:

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()
}

答案 5 :(得分:1)

您可以尝试在viewDidAppear而不是viewDidLoad中调用它。我有一个类似的错误,并修复它。

答案 6 :(得分:-1)

使用警报控制器:

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
 }
}