ios - 在 Swift 的 AppDelegate 中显示警报

标签 ios swift uialertcontroller

<分区>

我尝试下一个代码片段:

var alert = UIAlertController(title: "Alert", message: "Cannot connect to : \(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

在我的 AppDelegate 中,但它在控制台中打印出下一个错误:

Warning: Attempt to present <UIAlertController: 0x7ff6cd827a30> on <Messenger.WelcomeController: 0x7ff6cb51c940> whose view is not in the window hierarchy!

我该如何修复这个错误?

最佳答案

这就是我现在正在使用的。

var alertController = UIAlertController(title: "Title", message: "Any message", preferredStyle: .ActionSheet)
var okAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default) {
                    UIAlertAction in
                    NSLog("OK Pressed")
                }
var cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) {
                    UIAlertAction in
                    NSLog("Cancel Pressed")
                }
alertController.addAction(okAction)
alertController.addAction(cancelAction)
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)

关于ios - 在 Swift 的 AppDelegate 中显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32179710/

相关文章:

ios - 在 Swift 2.0 中控制任务

ios - iOS 中的警报 Controller 按钮颜色

ios - 在显示和关闭 `UIAlertView` 或 `UIAlertController` 时确定应用程序状态

ios - iOS/safari 上的 SVG 凸起,带有滤镜、feGaussianBlur 和 feColorMatrix

android - React-Native:-如何在单个目录中为 android 的不同分辨率提供图像

ios - 在 SwiftUI 中根据文本高度自动调整 View 高度

ios - 如何使 uitextfield 只接受数字和逗号

ios - 如何从 UI 测试中的标签检索变量数据

ios - SkSpriteNode没有成员(member)中心

swift - 如何更改来自结构的项目的 bool 值,从而更新 list ?