iOS:当用户看到视图后需要执行某些操作时,覆盖哪个函数?
因此,我的应用程序做的第一件事就是从API获取电影列表。我正在努力处理网络问题。目前,在我的viewDidLoad方法中,我调用“UpdateAppiInfo”,它包含以下代码:iOS:当用户看到视图后需要执行某些操作时,覆盖哪个函数?,ios,iphone,swift,Ios,Iphone,Swift,因此,我的应用程序做的第一件事就是从API获取电影列表。我正在努力处理网络问题。目前,在我的viewDidLoad方法中,我调用“UpdateAppiInfo”,它包含以下代码: if self.movies == nil { print("stuff went wrong") let alert = UIAlertController(title: "Network Error", message: "There was a nework err
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调用UpdateAppiInfo时,我收到以下消息:
Warning: Attempt to present <UIAlertController: 0x7fad10e3ad80> on <Flicks.MoviesViewController: 0x7fad10e35cc0> whose view is not in the window hierarchy!
警告:尝试显示其视图不在窗口层次结构中的对象!
当我仅从用户刷新中调用UpdateAppiInfo时,错误会按预期弹出
我假设viewDidLoad只在视图显示给用户之前被调用,我猜这是导致此错误的原因。是否有一种方法可以在向用户显示视图后插入此代码,以便解决问题?您需要使用
viewdide:
来确保视图已在窗口层次结构中
这里有一个很好的方法调用顺序的解释:
您需要使用
视图显示:
以确保视图已在窗口层次结构中
这里有一个很好的方法调用顺序的解释:
否,
viewDidLoad
在视图实例化时调用,但在视图显示之前调用。在视图实际呈现之后,您通常会使用viewdide
处理内容。没错viewDidLoad()
仅保证视图控制器的主视图被实例化(以及在Interface Builder中定义的任何子视图)。@CaptainForge-我下面的回答是否回答了您的问题?否,viewDidLoad
在视图被实例化时被调用,但在视图显示之前被调用。在视图实际呈现之后,您通常会使用viewdide
处理内容。没错viewDidLoad()
仅保证视图控制器的主视图被实例化(以及在Interface Builder中定义的任何子视图)。@CaptainForge-我下面的答案是否回答了您的问题?