使用 Swift 2.0 显示不带任何 UIButton 的 UIAlertController

Display a UIAlertController without any UIButton using Swift 2.0

我想答案很简单,但我已经尝试解决问题好几天了,但找不到解决方案。这是一个 UIViewController 在某些检查之后并且不使用任何按钮,显示一个 UIAlertController。始终出现 警告:试图呈现其视图不在 window 层次结构中!

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    noData()
}

func noData(){
    let alertController = UIAlertController(title: "Message", message: "There area no data in the file" , preferredStyle: .Alert)
    let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
    alertController.addAction(defaultAction)
    self.presentViewController(alertController, animated: true, completion: nil)
}

}

您只需要在 viewDidAppear 中调用您的方法 noData() 而不是 viewDidLoad。

override func viewDidAppear(animated: Bool) {
    noData()
}