簡體   English   中英

ViewController 在出現警報時重新加載 controller swift

[英]ViewController reloads on presenting alert controller swift

我正在做項目。 我在點擊注冊按鈕時添加了一個簡單的 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)
})

我通過這些代碼行解決了我的問題

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)
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM