无法在 Swift 的 UITableViewController 中显示 UIAlertController

Unable to present UIAlertController within a UITableViewController in Swift

我有一个 UIAlertController,我想在 UITableViewController 中显示它。但是,当我 运行 代码时,出现以下错误:

Warning: Attempt to present on whose view is not in the window hierarchy!

我的代码似乎反映了 中的建议。我做错了什么?

override func viewDidLoad() {
    super.viewDidLoad()

    // Check for force touch feature, and add force touch/previewing capability.
    if traitCollection.forceTouchCapability == .available {
        registerForPreviewing(with: self, sourceView: view)
    }
    else {
        // Create an alert to display to the user.
        alertController = UIAlertController(title: "3D Touch Not Available", message: "Unsupported device.", preferredStyle: .alert)
        self.present(alertController!, animated: false, completion: nil)
    }

假设,在 viewDidLoad 中,您仍然没有 window 层次结构中的视图,如消息中所述。尝试将您的代码移至 viewDidAppear 而不是