【问题标题】:iOS popup message in app delegate?应用程序委托中的iOS弹出消息?
【发布时间】:2017-10-30 05:52:35
【问题描述】:

我尝试在我的应用委托中创建弹出警报消息,但它根本没有显示。程序是用 Swift 4 编写的。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}

【问题讨论】:

标签: ios swift appdelegate uialertcontroller


【解决方案1】:

根据the documentation for didFinishLaunching

告诉代理启动过程几乎完成,应用几乎准备好运行。

所以我认为你不能在 didFinishLaunchingWithOptions 中显示警报。

尝试将您的代码移至applicationDidBecomeActive

【讨论】:

  • 是的,将我在 appdelgate swift 文件中的弹出消息移动到 func applicationDidBecomeActive。弹出窗口开始工作没有问题。感谢您发现错误...
【解决方案2】:

如果要在 iOS 原生编程中记住一条规则,那就是:UI 组件只能在主线程上正确操作。记住这一点,我相信它会让您在未来免于头痛。

 let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)

    let actionYes = UIAlertAction(title: "Yes", style: .default, handler: { action in
        print("Handler YES")
    })

    let actionCancel = UIAlertAction(title: "Cancel", style: .destructive, handler: { action in
        print("Handler CANCEL")
    })

    alert.addAction(actionYes)
    alert.addAction(actionCancel)

    DispatchQueue.main.async {
        self.window?.rootViewController?.present(alert, animated: true, completion: nil)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多