我刚刚创建了一个带有ViewController类的单视图应用程序项目。我想从我的自定义类中的一个函数中显示一个UIAlertController。
这是我的带有警报的类。
class AlertController: UIViewController {
func showAlert() {
var alert = UIAlertController(title: "abc", message: "def", preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion: nil)
}
}
这里是执行警报的ViewController。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func showAlertButton(sender: AnyObject) {
var alert = AlertController()
alert.showAlert()
}
}
我得到的不是一个漂亮的警告框,而是这个:
警告:尝试在Sprint1.AlertController: 0x797cc500中呈现UIAlertController: 0x797d2d20,但其视图不在窗口层次结构中!
我该怎么办?