返回 导航

Swift

hangge.com

Swift - whose view is not in the window hierarchy 问题解决方法

作者:hangge | 2015-03-10 09:48
问题现象:想在页面初始化的时候,使用self.presentViewController方法弹出个告警提示框UIAlertController。但行后报了个如下告警,同时告警框也出不来。
2015-03-10 09:55:34.197 Test[1140:29622] Warning: Attempt to present <UIAlertController: 0x7c95ca20> on <Test.ViewController: 0x7a6afc60> whose view is not in the window hierarchy!

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

class ViewController: UIViewController ,UIActionSheetDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        let alertController = UIAlertController(title: "系统提示",
                                                message: "您确定要离开hangge.com吗?",
                                                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)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
评论

全部评论(1)

回到顶部