【问题标题】:Present alert from AppDelegate when app is ready for presenting an alert当应用程序准备好呈现警报时,从 AppDelegate 呈现警报
【发布时间】:2018-07-17 15:10:31
【问题描述】:

我正在使用Xcode 9.4.1 (9F2000) 和Swift。

我在didFinishLaunchingWithOptions 中有这个AppDelegate:

DispatchQueue.main.asyncAfter(deadline: .now() + 20.0, execute: {
    self.selectUser()
})

selectUser() 将调用一个函数,该函数显示一个警报,让用户选择他的名字。

当立即调用selectUser() 时,ViewController(我猜)尚未准备好加载,我收到如下消息:

2018-07-17 16:51:42.576783+0200 MyApp[2492:824301] 警告: 尝试在 谁的观点不在 窗口层次结构!

我认为有更好的方法可以做到这一点,例如:如果应用已准备好加载并呈现 UIAlertController,请调用 selectUser()。

你知道怎么做吗?

【问题讨论】:

    标签: ios swift xcode uialertcontroller


    【解决方案1】:

    您需要 applicationDidBecomeActive ,但请确保在您从后台返回时也调用它,因此如果您不希望再次调用它,请将代码包装在一次 var 中

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

    【讨论】:

      【解决方案2】:

      由于您需要有一个根 viewController(故事板或编程),最简单的方法是在 viewController 中显示您的警报,因为如果完成了didFinishLaunchingWithOptions,您的“根”-viewController.view 将被显示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-06
        • 1970-01-01
        相关资源
        最近更新 更多