0
所以我的应用程序做的第一件事就是从API获取电影列表。我试图处理,如果有网络问题。目前,在我viewDidLoad方法,我称之为 “updateApiInfo”,它包含以下代码:iOS:在用户看到视图后需要执行某些操作时,应覆盖哪个函数?
if self.movies == nil {
print("stuff went wrong")
let alert = UIAlertController(title: "Network Error", message: "There was a nework error.\nYou must be connected to the internet to use Flicks.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Exit", style: UIAlertActionStyle.Default, handler: {(UIAlertAction) in
UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)
}))
self.presentViewController(alert, animated: true, completion: nil)
}
当viewDidLoad中调用updateApiInfo,我得到这个消息:
Warning: Attempt to present <UIAlertController: 0x7fad10e3ad80> on <Flicks.MoviesViewController: 0x7fad10e35cc0> whose view is not in the window hierarchy!
当我打电话updateApiInfo刚刚从用户刷新,错误按预期弹出。
我假设viewDidLoad只在之前被调用视图显示给用户,我猜是造成这个错误。有没有一种方法可以将这个代码粘贴到之后,这个视图就会显示给用户,所以这个问题可以想象得到修复吗?
没有,'当视图被实例化viewDidLoad'被调用,但呈现前。实际显示视图后,您通常会将'viewDidAppear'用于东西。 – Rob
没错。 'viewDidLoad()'只保证视图控制器的主视图被实例化(以及在Interface Builder中定义的任何子视图)。 –
@CaptainForge - 我的答案在下面回答你的问题? – siburb