0

I am trying to make a UIAlertController pop up one time when the user first downloads the app. However, I get the following error when I put the code in didFinishLaunchingWithOptions,

2015-01-15 23:54:45.306 WeddingApp Warning: Attempt to present UIAlertController:  on UITabBarController: whose view is not in the window hierarchy!

My code is below:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Override point for customization after application launch.
        if window == nil {
        println("window is nil");
        return true;
    }

    if window!.rootViewController == nil {
        println("window!.rootViewController is nil");
        return true;
    }

    //TabBarController//
    let tabBarController: UITabBarController = window!.rootViewController! as UITabBarController;
    UITabBar.appearance().tintColor = UIColor.magentaColor();
    UITabBar.appearance().translucent = false;

/* tableView cells are now completely visable. They do not hie behind the tabBar */

    tabBarController.viewControllers = [
        TwelveToTenMonths(nibName: nil, bundle:nil),
        NineToSevenMonths(nibName: nil, bundle:nil),
        SixToFourMonths(nibName: nil, bundle:nil),
        ThreeToOneMonth(nibName: nil, bundle:nil),
    ];



    var alert = UIAlertController(
                title: "Welcome to WeddingApp!",
                message: "Thank You for choosing the \nWedding App for your organization needs. \n\nFor more great tools please visit our website www.wedme.com",
                preferredStyle: UIAlertControllerStyle.Alert);

    alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.Default, handler: nil));

    self.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil);


    return true
}

***NOTE: I also tried adding the following to the appDelegate instead of the above code but the alert continues to appear whenever I return to the app.....

func applicationDidBecomeActive(application: UIApplication) { //Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    showAlert();

}

func showAlert(){    
    println("alert");
    var alert = UIAlertController(
        title: "Welcome to WeddingApp!",
        message: "Thank You for choosing the \nWedding App for your organization needs. \n\nFor more great tools please visit our website www.wedme.com",
        preferredStyle: UIAlertControllerStyle.Alert);

    alert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.Default, handler: nil));

    self.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil);

}

Does anyone know a way around this???

Jack
  • 1
  • 3

2 Answers2

0

In the first case, you never added the UIAlertController (you could try adding it as a child view controller).

https://stackoverflow.com/a/17012439/3324388

In the second case you assigned the root controller to the UIAlertController and never assign the root controller back to the UITabBarController. You need to set it back.

Have you tried using a UIAlertView instead?

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/index.html

Community
  • 1
  • 1
Aggressor
  • 13,323
  • 24
  • 103
  • 182
  • regarding the first part of your answer... I used the following line of code to add the UIAlertController, self.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil); ... in response to your second answer UIAlertView is deprecated in iOS 8. – Jack Jan 16 '15 at 21:40
0

Try something like this:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

    if self.window!.rootViewController as? UITabBarController != nil {
        var tababarController = self.window!.rootViewController as UITabBarController
        .
        .
        .

        let alertController = UIAlertController(title: "Welcome...", message: "Thank You...", preferredStyle: .Alert)
        tababarController.presentViewController(alert, animated: true, completion: nil);
    }

    return true
}
Juan de la Torre
  • 1,297
  • 9
  • 19
  • My alert doesn't pop up with this code. I also get the following error ... 2015-01-16 09:59:58.789 WeddingApp[748:12798] Warning: Attempt to present UIAlertController: 0x7fdbf2e54580 on UITabBarController: 0x7fdbf2c7f810 whose view is not in the window hierarchy! 2015-01-16 09:59:58.790 WeddingApp[748:12798] Warning: Attempt to present UIAlertController: 0x7fdbf2e54580 on UITabBarController: 0x7fdbf2c7f810 whose view is not in the window hierarchy! – Jack Jan 16 '15 at 15:02
  • I explained your issue in my answer. You are trying to present a view controller without it being added to the window! You need to add the view controllers view to the window! – Aggressor Jan 16 '15 at 18:27
  • @Aggressor is right, you might haven't added the tab bar controller to the window, are you adding it with something like this? window?.rootViewController = tabBarController – Juan de la Torre Jan 17 '15 at 19:41