viewDidLoad和viewDidAppear之间的区别 [英] Difference between viewDidLoad and viewDidAppear

查看:92
本文介绍了viewDidLoad和viewDidAppear之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

viewDidLoad viewDidAppear 之间有什么区别?什么样的初始化或自定义代码进入这些函数?

What is the difference between viewDidLoad and viewDidAppear? What kind of initialization or custom code goes into those functions?

例如。 presentModalViewController 仅在 viewDidAppear 中出现,而不在 viewDidLoad 上。

e.g. presentModalViewController works only when present in viewDidAppear and not on viewDidLoad.

推荐答案

viewDidLoad 只调用一次,当视图控制器是先加载到内存中。这是您希望实例化任何实例变量并构建在此视图控制器的整个生命周期中存在的任何视图的位置。但是,此时视图通常尚不可见。

viewDidLoad is called exactly once, when the view controller is first loaded into memory. This is where you want to instantiate any instance variables and build any views that live for the entire lifecycle of this view controller. However, the view is usually not yet visible at this point.

viewDidAppear 在视图实际可见时被调用,并且可以在视图控制器的生命周期中多次调用(例如,当模式视图控制器被关闭并且视图再次可见时)。您可以在此处执行任何布局操作或在UI中执行任何绘图 - 例如,呈现模态视图控制器。但是,你在这里做的任何事都应该是可重复的。最好不要在这里保留内容,否则如果在视图消失时不释放内存,就会导致内存泄漏。

viewDidAppear is called when the view is actually visible, and can be called multiple times during the lifecycle of a View Controller (for instance, when a Modal View Controller is dismissed and the view becomes visible again). This is where you want to perform any layout actions or do any drawing in the UI - for example, presenting a modal view controller. However, anything you do here should be repeatable. It's best not to retain things here, or else you'll get memory leaks if you don't release them when the view disappears.

请参阅: https://developer.apple.com/documentation/uikit/uiviewcontroller

这篇关于viewDidLoad和viewDidAppear之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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