无法在 swift 中显示 UIAlertController

Cannot present UIAlertController in swift

我有这个功能

func showMessageNew(msg:String,title: String){
        let alertController = UIAlertController(title: title, message: msg, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default)
        alertController.addAction(okAction)
        self.present(alertController, animated: true)
}

但是当我 运行 它时,我在控制台中有这个:whose view is not in the window hierarchy!

下面的代码可以用来表示UIAlertController,

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            self.showMessageNew(msg: "hi", title: "hi")
            }
        }

    func showMessageNew(msg:String,title: String){
        let alertController = UIAlertController(title: title, message: msg, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default)
        alertController.addAction(okAction)
        self.present(alertController, animated: true)
    }
}