Swift - whose view is not in the

2020-07-02  本文已影响0人  小驴拉磨

问题:想在页面初始化的时候,使用self.presentViewController方法弹出个告警提示框UIAlertController。但行后报了个如下告警,同时告警框也出不来。

2020-07-02 11:52:36.397992+0800 弹射按钮[30600:5157611] Warning: Attempt to present <UIAlertController: 0x7ff87d033000> on <ºπÂ∞ÑÊåâÈíÆ.ViewController: 0x7ff87ce08e50> whose view is not in the window hierarchy!

解决办法:原来的调用代码是写在viewDidLoad方法中,这个表示视图加载完毕。我们应该把方法调用放到viewDidApper中,这个是UIViewController对象的视图已经加入到窗口时调用。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    showAlertController()
}

func showAlertController()
{
    let alertController = UIAlertController(title: "系统提示",
                                                    message: "您确定喜欢打开小驴拉磨",
                                                    preferredStyle: .alert)
    let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
    let okAction = UIAlertAction(title: "好的", style: .default, handler: nil)
    alertController.addAction(cancelAction)
    alertController.addAction(okAction)
    self.present(alertController, animated: true, completion: nil)

}
上一篇下一篇

猜你喜欢

热点阅读