swift - 我如何让这个 UIAlert 出现

标签 swift uialertcontroller

我正在尝试通过按下“删除数据”按钮来发出警报。我的按钮位于导航 Controller 内的 View 中。这是我当前的代码

class SomeViewController: UIViewController {
    @IBAction func deleteData(sender: UIButton) {
        let alert = UIAlertController(title: "delete Data", message: "Do you want to delete the data", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: nil))
        alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.Default, handler: nil))
    }
}

但是给我以下错误:

Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior

最佳答案

您已经创建了警报 View Controller ,但您仍然需要展示它:

self.presentViewController(alert, animated: true, completion: nil)

您可以在 appcoda.com 上阅读有关该主题的文章

关于swift - 我如何让这个 UIAlert 出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34926852/

相关文章:

ios - 如何覆盖导航栏

ios - UIAlertController 的外观

ios - 如何更改 UIAlertController 的背景颜色?

ios - 加载 TableView 时关闭 UIAlertController

swift - 如何从 UIAlertController 的 UIAlertAction 处理程序中关闭 UIViewController?

xcode - 如何在 Swift 中的文件之间共享常量和变量

swift - 如何关闭模态并同时发送数据并执行segue

ios - 在 iOS Swift 中将 NSDate 转换为字符串

ios - 快速http请求无法获得非200响应

swift - 如何使用 swift 通过 UIAlertAction 打开输入附件 View ?