【问题标题】:UIAlert Controller not working in AppDelegte didFinishLaunchingWithOptions method - in SwiftUIAlertController 在 AppDelegate didFinishLaunchingWithOptions 方法中不起作用 - 在 Swift 中
【发布时间】:2015-01-16 05:04:56
【问题描述】:

我试图在用户第一次下载应用程序时弹出一个 UIAlertController。但是,当我将代码放入 didFinishLaunchingWithOptions 时,出现以下错误,

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

我的代码如下:

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
}

***注意:我还尝试将以下代码添加到 appDelegate 而不是上面的代码,但是每当我返回应用程序时,警报都会继续出现.....

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);

}

有谁知道解决这个问题的方法???

【问题讨论】:

标签: ios swift appdelegate alerts uialertcontroller


【解决方案1】:

在第一种情况下,您从未添加过 UIAlertController(您可以尝试将其添加为子视图控制器)。

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

在第二种情况下,您将根控制器分配给 UIAlertController,而从未将根控制器分配回 UITabBarController。您需要重新设置它。

您是否尝试过使用 UIAlertView 代替?

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

【讨论】:

  • 关于您答案的第一部分...我使用以下代码行添加 UIAlertController, self.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil) ; ...响应您的第二个答案 UIAlertView 在 iOS 8 中已弃用。
【解决方案2】:

试试这样的:

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
}

【讨论】:

  • 我的警报没有弹出此代码。我还收到以下错误... 2015-01-16 09:59:58.789 WeddingApp[748:12798] 警告:尝试在 UITabBarController 上呈现 UIAlertController:0x7fdbf2e54580:0x7fdbf2c7f810,其视图不在窗口层次结构中! 2015-01-16 09:59:58.790 WeddingApp[748:12798] 警告:尝试在 UITabBarController 上呈现 UIAlertController:0x7fdbf2e54580:0x7fdbf2c7f810,其视图不在窗口层次结构中!
  • 我在回答中解释了您的问题。您正在尝试在不将其添加到窗口的情况下呈现视图控制器!您需要将视图控制器视图添加到窗口!
  • @Aggressor 是对的,你可能还没有将标签栏控制器添加到窗口中,你是用这样的东西添加它吗? window?.rootViewController = tabBarController
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2021-12-17
  • 2018-12-12
  • 2020-06-20
  • 1970-01-01
相关资源
最近更新 更多