ios - 警告时 UIAlertController : 0x7ff211650140 called in Swift

标签 ios swift uialertcontroller

我有时在调用 UIAlert 时收到此警告

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

code used:

    func alert (dictKey: String){
        print(dictKey)
        let alertController = UIAlertController(title: nil, message: promptsArr[dictKey], preferredStyle: .Alert )
        let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in

        }
        alertController.addAction(OKAction)
        if self.presentedViewController == nil {
            delay(1.0){
                self.presentViewController(alertController, animated: true, completion: nil)
            }
        } else {
            self.presentedViewController!
        }

最佳答案

  1. 移除delay()

  2. 将逻辑放在 self.presentedViewController 中: (如果存在则无需创建警报)

    func alert (dictKey: String){
        if self.presentedViewController == nil {
            let alertController = UIAlertController(
                title: nil,
                message: promptsArr[dictKey],
                preferredStyle: .Alert )
            let OKAction = UIAlertAction(title: "OK", style: .Default) {
                (action) in
            }
            alertController.addAction(OKAction)
    
            self.presentViewController(
                alertController,
                animated: true,
                completion: nil)
        }
    }
    

关于ios - 警告时 UIAlertController : 0x7ff211650140 called in Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33705748/

相关文章:

objective-c - UItextField - AlertView 上的 iOS 多行文本字段

ios - 想要将 UIAlert 移动到 View 的顶部

ios - 使用各种控件(按钮、开关和文本字段)在 TableView 中实现上一个/下一个按钮

ios - 如何在 alamofire 多部分数据中传递字典? iOS swift3

ios - 在 Swift 3 中创建具有自定义大小的 VideoPlayer

swift - 从 SwiftUI 的列表中删除默认填充

macos - Swift 绑定(bind)到 Objective-C bool 值

ios - Gmail 集成时出现链接器错误

ios - 测试可选的 nil 并将 nil 切换为 0

ios - Swift - 在 VC 中登录,允许任何人输入而不验证用户名和密码匹配或保存