About viewController's "viewDidLoad" and "viewWillAppear" methods
Asked Answered
N

5

6

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.

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?

Thanks!

Norvol answered 28/6, 2013 at 10:2 Comment(1)
"viewDidLoad is called when the viewController is loaded for first time (as the name indicates)" - It confused me for a long time too. The view controller and its view are different things. This is one of the most important (and fundamental) things you have to understand about view controllers.Gide
M
20

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:

  • 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.

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

Mcshane answered 28/6, 2013 at 10:18 Comment(2)
That UIViewController lifecycle diagram is superb. Thanks for the link.Megara
It's worth noting that viewWillUnload is deprecated in iOS 6 ("views are no longer purged under low-memory conditions and so this method is never called"). The bottom-left part of the Lifecycle Diagram doesn't apply to modern iOS apps. The rest of the diagram is very useful though, thanks!Chemisorption
O
9

-viewDidLoad is called when the controller loads its view, which is not necessarily right after initialization. View controllers don't load their views until they need them, either to display or for any other reason.

-viewWillAppear is called just before the view is displayed. This will be after -viewDidLoad, but you don't know exactly how long after. -viewWillAppear is called every time the view is displayed; -viewDidLoad will only be called a second time if the view is unloaded at some point (such as didReceiveMemoryWarning). These days that's unusual, but it can happen.

Or if the viewController is set to nil, which can usually happen if a view controller is kicked off the navigation stack, and therefore next time it is brought to the navigation stack it needs to call -viewDidLoad again.

Orthodontist answered 28/6, 2013 at 10:8 Comment(1)
also, all your Outlets that you might have set up in Interface Builder are hooked up, when viewDidLoad is called.Apsis
B
3

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

No. The name indicates that the controller's view has been loaded (not the controller itself). Actually the docs state that this method will get called after the view hierarchy has been loaded into memory (either via loadView or through a nib for example).

Then, I thought that once viewDidLoad returns, viewWillAppear is called

Again, no. loadView (and as a consequence viewDidLoad) method will get called the first time that view property is to be accessed and is nil (which is the case when you're initializing a controller). Think of this simple scenario:

MyViewController *vc = [[MyViewController alloc] init];
UIView *view = vc.view; // <= loadView & viewDidLoad will fire but it certainly didn't appear...

However, while developing I make the impression that there is no order when calling viewDidLoad and viewWillAppear...

Well there is an order. We know for sure that viewWillAppear will always be called after viewDidLoad (if both of them are to be called of course).

Briones answered 28/6, 2013 at 10:35 Comment(2)
So, the point is that there is an order if both methods are called, but viewDidLoad hasn't necessarily to return before viewWillAppear being called?Norvol
The point is that the methods aren't 'linked' in any way. viewDidLoad is about loading the hierarchy into the memory, while viewWillAppear is called when the view is about to be added into a hierarchy (for presentation). viewDidLoad always returns before viewWillAppear.Briones
T
1

As you said, ViewDidLoad is only calling once after loading the view. So we can initialize the instances in the viewDidLoad. It is mainly meant for the initialization.

viewWillAppear will invoke whenever we reach to this view. So if there is any changes in UI, we can done it in viewWillAppear.

Tun answered 28/6, 2013 at 10:10 Comment(0)
L
0

I ran a trace on when all these calls are made: http://thecodist.com/article/ios_arc_storyboards_and_uiviewcontroller_trace

Laflamme answered 28/6, 2013 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.