0

我正在做项目。我在点击注册按钮时添加了一个简单的 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)
})
4

1 回答 1

0

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

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-07-01T13:56:13.823 回答