ios viewDidLoad 和 viewDidAppear 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11254697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between viewDidLoad and viewDidAppear
提问by user462455
What is the difference between viewDidLoad
and viewDidAppear
? What kind of initialization or custom code goes into those functions?
viewDidLoad
和 和有viewDidAppear
什么区别?什么样的初始化或自定义代码进入这些函数?
e.g. presentModalViewController
works only when present in viewDidAppear
and not on viewDidLoad
.
例如,presentModalViewController
仅当存在于viewDidAppear
而不存在于 时才起作用viewDidLoad
。
回答by davidgoli
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.
viewDidLoad
当视图控制器第一次加载到内存中时,只调用一次。这是您想要实例化任何实例变量并构建在此视图控制器的整个生命周期中存在的任何视图的地方。但是,此时该视图通常还不可见。
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.
viewDidAppear
在视图实际可见时调用,并且可以在视图控制器的生命周期中多次调用(例如,当模态视图控制器被关闭并且视图再次变为可见时)。这是您想要在 UI 中执行任何布局操作或进行任何绘图的地方 - 例如,呈现一个模态视图控制器。但是,您在这里所做的任何事情都应该是可重复的。最好不要在这里保留东西,否则如果视图消失时不释放它们,则会导致内存泄漏。
See: https://developer.apple.com/documentation/uikit/uiviewcontroller
请参阅:https: //developer.apple.com/documentation/uikit/uiviewcontroller
回答by WrightsCS
Simply put, you would want to create any controls or arrays in viewDidLoad
, where as in viewDidAppear
is where you would want to refresh those controls or arrays.
简而言之,您可能希望在 中创建任何控件或数组viewDidLoad
,而 inviewDidAppear
是您希望刷新这些控件或数组的位置。
viewDidLoad
is called once when the controller is created and viewDidAppear
is called each time the view, well, DID appear. So say you have a modal view that you present, when that view is dismissed, viewDidAppear
will be called, and viewDidLoad
will not be called.
viewDidLoad
在创建控制器时调用一次,并viewDidAppear
在每次视图时调用,好吧,DID 出现。因此,假设您有一个模态视图,当该视图被关闭时,viewDidAppear
将被调用,并且viewDidLoad
不会被调用。