关于 viewController 的“viewDidLoad"和“viewWillAppear"方法 [英] About viewController's "viewDidLoad" and "viewWillAppear" methods

查看:16
本文介绍了关于 viewController 的“viewDidLoad"和“viewWillAppear"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于上述两种方法的问题,因为在我的测试中我没有明确它们的调用顺序.我认为,首先, viewDidLoad 在第一次加载 viewController 时调用(如名称所示),然后在 init 方法之后调用.然后,我认为一旦 viewDidLoad 返回,就会调用 viewWillAppear.如果你显示另一个viewController,然后你又回到这个viewController,那么它应该已经被加载并且只有viewWillAppear 会被调用.

I've got a question regarding the two mentioned methods, since in my tests I don´t make clear the order they are called. I thought that, firstly, viewDidLoad is called when the viewController is loaded for first time (as the name indicates), and inmediately after the init method. Then, I thought that once viewDidLoad returns, viewWillAppear is called. If you display another viewController, and then you return to this one, then it should be already loaded and only viewWillAppear will be called.

但是,在开发过程中,我给人的印象是调用 viewDidLoadviewWillAppear 时没有顺序......我在Apple 的文档,这实际上是如何工作的?

However, while developing I make the impression that there is no order when calling viewDidLoad and viewWillAppear... I couldn´t find a clear description of this lifecycle in Apple's documentation, how does this actually work?

谢谢!

推荐答案

我想补充 Caleb 的回答:不要混淆视图控制器和视图!名称 viewDidLoad 清楚地表明该方法是在加载 view 后调用的.执行加载的是视图控制器.

I would like to add to Caleb's answer: Don't confuse the view controller and the view! The name viewDidLoad clearly indicates that the method is invoked after the view has been loaded. It is the view controller that does the loading.

关于视图生命周期和消息发送顺序的一些提示:

Some pointers regarding the lifecycle of views and the order in which messages are sent:

  • 不是 Apple 的官方文档,但我发现这张图表非常有用,因为它包括几乎所有 UIViewController 的生命周期覆盖.
  • 在 Apple 的View控制器编程指南》有一个流程图,描述了视图最初是如何加载的.它解释了 loadViewviewDidLoad,也结合故事板.
  • 响应显示相关通知部分来自 Apple 的视图控制器编程指南"解释了如何响应出现和消失的视图(viewWillAppear: 等)
  • 如果您计划实施容器视图控制器:UIViewController 类参考很好地概述了您的子类需要如何发送消息.
  • Not an official Apple document, but I find this diagram really useful because it includes pretty much all of UIViewController's lifecycle overrides.
  • In the section Resource Management in View Controllers from Apple's "View Controller Programming Guide" there is a flowchart that depicts how views are initially loaded. It explains loadView and viewDidLoad, also in conjunction with storyboards.
  • The section Responding to Display-Related Notifications from Apple's "View Controller Programming Guide" explains how to respond to views appearing and disappearing (viewWillAppear: et al)
  • If you are planning on implementing a container view controller: The UIViewController class reference has a good overview of how messages need to be sent by your subclass.

我就到这里了.你可以自己通过谷歌搜索uiviewcontroller 生命周期"找到更多的东西.

I'm stopping here. You can find more stuff yourself by googling for "uiviewcontroller life cycle".

这篇关于关于 viewController 的“viewDidLoad"和“viewWillAppear"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆