[英]How to make viewDidLoad called more than once
I know that viewDidLoad
method may be called multiple times during UIViewController
's lifecycle. 我知道在
UIViewController
的生命周期中可能会多次调用viewDidLoad
方法。 But how is that possible? 但那怎么可能呢? How to make it called more than once not calling it directly?
如何让它不止一次调用而不是直接调用它? I tried doing it this way:
我试过这样做:
UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
view.backgroundColor = [UIColor greenColor];
self.view = view;
and whereas my view is actually changed, viewDidLoad
is not called. 虽然我的视图实际上已更改,但未调用
viewDidLoad
。 Can anyone give an example? 谁能举个例子?
The first time you access a viewController's view
property the view will be created with loadView
and then you will receive the viewDidLoad
call. 第一次访问viewController的
view
属性时,将使用loadView
创建视图,然后您将收到viewDidLoad
调用。
You will not receive the viewDidLoad
call again unless the view is destroyed - this may occur if your viewController goes off screen and UIKit
decides to purge any view's that are not visible. 除非视图被破坏,否则您不会再次收到
viewDidLoad
调用 - 如果您的viewController关闭屏幕并且UIKit
决定清除任何不可见的视图,则可能会发生这种情况。 Thus next time you access the view
property it will notice it does not exist and again create one with loadView
and then call viewDidLoad
. 因此,下次访问
view
属性时,它会注意到它不存在,并再次使用loadView
创建一个,然后调用viewDidLoad
。
viewWillAppear method is an UIViewController method. viewWillAppear方法是一个UIViewController方法。 Why you shouldn't call directly?
为什么你不应该直接打电话?
By the way there is no way to do that, while you assign an UIView to your self.view, id you are not doing it in the init or in the loadView or didLoad methods.. 顺便说一下,没有办法做到这一点,当你为你的self.view指定一个UIView时,id你不是在init或loadView或didLoad方法中做的。
the life cycle is that: 生命周期是:
Then you present the view and: 然后你呈现视图和:
if you want to change the view during your uiviewcontroller life cycle you should do: 如果你想在你的uiviewcontroller生命周期中更改视图,你应该这样做:
UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
view.backgroundColor = [UIColor greenColor];
[self viewWillAppear:NO]; //set to yes if you are making some kind of animation
self.view = view;
[self viewDidAppear:NO];
The will disappear and did disappear will be called according to the UIVIewController life cycle. 根据UIVIewController生命周期,将消失并消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.