Ios UIAlertController仅在ViewDidDisplay完成其调用时可见

Ios UIAlertController仅在ViewDidDisplay完成其调用时可见,ios,swift,uialertcontroller,Ios,Swift,Uialertcontroller,我试图在视图加载时显示UIAlertController。我知道这在viewdiload()或viewwillispect()期间是不可能的,因为在执行这些函数期间,视图不在层次结构中。但是,如果视图添加在viewdide()之前,则在调用该函数期间,应该可以显示myUIAlertController 因此,我尝试了以下方法: override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated)

我试图在视图加载时显示
UIAlertController
。我知道这在
viewdiload()
viewwillispect()
期间是不可能的,因为在执行这些函数期间,视图不在层次结构中。但是,如果视图添加在
viewdide()
之前,则在调用该函数期间,应该可以显示my
UIAlertController

因此,我尝试了以下方法:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    var AlertShow = false
    print(Configuration)

    if !AlreadyLoad {

        if Configuration.count == 0 {
            AlertShow = true
            print("First lunch")
            let Alert = UIAlertController(title: "Premier lancement", message: "C'est la première fois que l'application est lancée. Cette dernière va télécharger tous les articles.\nVeuillez patienter.", preferredStyle: .Alert)
            let OkAction = UIAlertAction(title: "Ok", style: .Default) { (action) in }
            Alert.addAction(OkAction)
            self.presentViewController(Alert, animated: true, completion: {
                AlertShow = false
            })
        }
}
但我的警报只有在执行
viewdide()之后才可见。
我想在开始时显示此警报


我正在使用Swift 2、Xcode 7和iOS 9 SDK。

呈现独立于任何视图控制器的UIAlertController的方法是创建新的UIWindow和rootViewController,并放在当前UIWindow的顶部。这里推荐这种方法


如果您想查看实现。你可以看看这里。它很简单,但效果很好。我将想法转化为目标c,并在我的应用程序中使用它。这里是Dylan Bettermann的swift版本:

呈现独立于任何视图控制器的UIAlertController的方法是创建新的UIWindow和rootViewController,并将其置于当前UIWindow之上。这里推荐这种方法


如果您想查看实现。你可以看看这里。它很简单,但效果很好。我将想法转化为目标c,并在我的应用程序中使用它。这是Dylan Bettermann的swift版本:

我尝试了DBAlertController方法,但出现了另一个错误,我这样做了:

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    //Notification demand
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
    File.changeCurrentDirectoryPath(Path[0])
    CreateFile.changeCurrentDirectoryPath(Path[0])
    UIApplication.sharedApplication().canOpenURL(NSURL(fileURLWithPath: "fbauth://authorize"))
    //deleteAllFile()
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window!.rootViewController = UINavigationController(rootViewController: ViewController2())

    window!.makeKeyAndVisible()
    return true
}
在viewController2()中,我执行以下操作:

@IBOutlet var OptionButton: UIBarButtonItem!
override func viewDidLoad() {
    super.viewDidLoad()

    ReadAllFile()
    let alertController = DBAlertController(title: "DBAlertController", message: "Hello World!", preferredStyle: .Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    alertController.show(animated: true, completion: nil)
    OptionButton.title = "\u{2699}" }

但是我的OptionButton是nil(我链接了它),我出现了致命错误。

我尝试了DBAlertController方法,但出现了另一个错误,我这样做了:

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    //Notification demand
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil))
    File.changeCurrentDirectoryPath(Path[0])
    CreateFile.changeCurrentDirectoryPath(Path[0])
    UIApplication.sharedApplication().canOpenURL(NSURL(fileURLWithPath: "fbauth://authorize"))
    //deleteAllFile()
    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window!.rootViewController = UINavigationController(rootViewController: ViewController2())

    window!.makeKeyAndVisible()
    return true
}
在viewController2()中,我执行以下操作:

@IBOutlet var OptionButton: UIBarButtonItem!
override func viewDidLoad() {
    super.viewDidLoad()

    ReadAllFile()
    let alertController = DBAlertController(title: "DBAlertController", message: "Hello World!", preferredStyle: .Alert)
    alertController.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    alertController.show(animated: true, completion: nil)
    OptionButton.title = "\u{2699}" }

但是我的OptionButton是nil(我链接了它),我出现了致命错误。

之后您是否尝试过使用dispatch\u?“但我的警报只有在执行view ViewDidDisplay()后才可见。”我不太清楚这一点。您使用的是导航控制器还是类似的控制器?如果是这样,您可以使警报显示在它上面,而不是显示在您当前的视图控制器上。我使用的是Tab Bar控制器,哪个函数用于显示我的Tab Bar控制器?我会尝试一下,我测试了一些代码,没有收到错误消息。我的警报在这里,但在函数viewdide结束时出现。在presentviewcontroller之后,我测试了打印(“某物”),我收到了打印的消息,然后我收到了警报(因此,在ViewDidDisplay的末尾),您是否在之后尝试了dispatch_?“但我的警报只有在执行view ViewDidDisplay()后才可见。”我不太清楚这一点。您使用的是导航控制器还是类似的控制器?如果是这样,您可以使警报显示在它上面,而不是显示在您当前的视图控制器上。我使用的是Tab Bar控制器,哪个函数用于显示我的Tab Bar控制器?我会尝试一下,我测试了一些代码,没有收到错误消息。我的警报在这里,但在函数viewdide结束时出现。在presentviewcontroller之后,我测试了打印(“某物”),我收到了打印的消息,然后收到了警报(因此,在ViewDidDisplay的末尾)