简体   繁体   English

如何使viewDidLoad多次调用

[英]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: 生命周期是:

  • init 在里面
  • loadView //change your view here loadView //在这里更改您的视图
  • viewDidLoad viewDidLoad中

Then you present the view and: 然后你呈现视图和:

  • viewWillAppear: viewWillAppear中:
  • viewDidAppear: viewDidAppear:

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.

相关问题 在UITabBarController中是否多次调用viewDidLoad? - Does viewDidLoad get called more than once in UITabBarController? iOS 6中的viewDidLoad调用一次? - viewDidLoad in iOS 6 called once? viewDidLoad,它仅被调用一次吗? - viewDidLoad, is it called only once? 如何使SwrevealViewController的viewDidLoad仅调用一次 - how to make viewDidLoad of SwrevealViewController call only once 如何防止collectionView:didSelectItemAtIndexPath委托方法多次调用 - How to prevent collectionView:didSelectItemAtIndexPath delegate method called more than once application:didReceiveRemoteNotification:fetchCompletionHandler:多次调用。如何避免? - application: didReceiveRemoteNotification: fetchCompletionHandler: called more than once.How to avoid? viewDidLoad() 和 viewDidAppear() 只调用一次? - viewDidLoad() and viewDidAppear() called only once? didAddAnnotationViews被多次调用 - didAddAnnotationViews is being called more than once 如何使ViewController的viewDidLoad方法仅调用一次? - How to make viewDidLoad method of ViewController call only once? 在多次调用applicationWillResignActive之后游戏崩溃 - Game Crashes after applicationWillResignActive is called more than once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM