【问题标题】:ViewController reloads on presenting alert controller swiftViewController 在快速呈现警报控制器时重新加载
【发布时间】:2020-10-19 20:02:46
【问题描述】:

我正在做项目。我在点击注册按钮时添加了一个简单的 alertController。当我单击按钮时,我的视图控制器会重新加载,然后它会显示该警报控制器。它发生在 iOS 13 和 swift 5 或更高版本上

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

     let ok = UIAlertAction(title: "OK", style: .default, handler: { action in
     })
     alert.addAction(ok)
     let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in
     })
     alert.addAction(cancel)
     DispatchQueue.main.async(execute: {
        self.present(alert, animated: true)
})

【问题讨论】:

  • 如果您在问题中显示您的代码,有人可能会找到解决方案。
  • 代码很简单,我只是在 IBACTION 中调用一个简单的警报控制器
  • 但是让我用代码更新我的问题
  • @PhillipMills 请立即查看。
  • 在示例应用程序中尝试了您的代码,但没有发现问题。视图控制器没有变化,没有重新加载。 (斯威夫特 5,iOS 13.5)

标签: ios uiviewcontroller ios13 swift5 uialertviewcontroller


【解决方案1】:

我通过这几行代码解决了我的问题

func alert(message: String, title: String = "") {
            let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
            let alertAction = UIAlertAction(title: "OK", style: .default) { (action) in
                print("Action")
            }
            alertController.addAction(alertAction)
           (UIApplication.shared.delegate as! AppDelegate).alertWindow.isHidden = true

            self.presentViewController(alertController: alertController)
    //        self.present(alertController, animated: true, completion:  nil)

        }
    func presentViewController(alertController: UIAlertController, completion: (() -> Void)? = nil) {
        if var topController = UIApplication.shared.keyWindow?.rootViewController {
            while let presentedViewController = topController.presentedViewController {
                topController = presentedViewController
            }

            DispatchQueue.main.async {
                topController.present(alertController, animated: true, completion: completion)
            }
        }
    }

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多