这个问题通常出现在后台线程或异步操作中,在这些情况下,AlertController 将无法被添加到窗口层次结构中。以下是一个解决方法的示例代码,将 AlertController 的显示操作修改为在主线程中完成即可避免出现此问题:
DispatchQueue.main.async {
let alertController = UIAlertController(title: "提示", message: "这是一个提示框", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "确定", style: .default, handler: nil)
alertController.addAction(confirmAction)
self.present(alertController, animated: true, completion: nil)
}