仅当viewDidAppear完成调用时,UIAlertController才可见 [英] UIAlertController visible only when viewDidAppear has finished its call

查看:202
本文介绍了仅当viewDidAppear完成调用时,UIAlertController才可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的视图加载时显示 UIAlertController 。我知道在 viewDidLoad() viewWillAppear()期间是不可能的,因为在执行期间视图不在层次结构中这些功能。但是,如果视图是在 viewDidAppear()之前添加的,那么应该可以在通话期间显示我的 UIAlertController 该函数。

I am trying to show a UIAlertController as my view loads. I know that's impossible during viewDidLoad() or viewWillAppear() because the view is not in the hierarchy at during the execution of those functions. However, if the view is added before viewDidAppear(), then it should be possible to show my UIAlertController during the call of that function.

因此,我试过这个:

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

但我的警报仅在执行 viewDidAppear()后才可见。
我想在开头显示此提醒。

But my alert is only visible after the execution of viewDidAppear(). I would like show this alert at the beginning.

我使用的是Swift 2,Xcode 7和iOS 9 SDK。

I am using Swift 2, Xcode 7, and the iOS 9 SDK.

推荐答案

呈现独立于任何视图控制器的UIAlertController的方法是创建新的UIWindow和rootViewController并置于当前的UIWindow之上。这里推荐使用此方法。 如何在不在视图控制器中时显示UIAlertController?

The way to present UIAlertController that is independent to any view controller is to create the new UIWindow and rootViewController and put on top of the current UIWindow. This method is recommended here. How to present UIAlertController when not in a view controller?

如果你想看到实现。你可以看看这里。它很简单,但效果很好。我将这个想法转换为目标c并在我的应用程序中使用它。以下是Dylan Bettermann的快速版本: https://github.com/dbettermann/DBAlertController

If you want to see the implementation. You could take a look here. It simple but works very well. I converted the idea to objective c and use this in my app as well. Here is the swift version by Dylan Bettermann: https://github.com/dbettermann/DBAlertController

这篇关于仅当viewDidAppear完成调用时,UIAlertController才可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆