I'm looking into the viewDidLoad and viewDidAppear methods to better understand what they both do and I came across an article which uses the example of a banking application to explain how these methods work:
Consider a banking application that shows your current balance. A user can tap on a button, which presents a list of nearby ATMs in a modal view controller. In order to get the list of nearby ATMs, the application must make a core location and web service request.
In this situation, a programmer could get away with requesting the list of nearby ATMs from the server in viewDidLoad. The view controller is only presented once, both viewDidLoad and viewWillAppear: will be called back to back and only once for that particular view controller instance. The net effect of the code in either of these two methods will be the same.
But this is a bad idea. Consider what would happen if you wanted to move the ATM view controller into a tab bar controller. Now, the ATM view controller – with its ATM fetching code in viewDidLoad only fetches the list of ATMs once. So you are in Atlanta on Tuesday, open up your application to look for an ATM, then check your balance. Then you travel to New York on Wednesday, re-open the banking application, and you only see ATMs in Atlanta. The view was already loaded, no need to call viewDidLoad and now you’re looking at stale data.
Sadly, I still don't fully understand how/why both viewDidLoad and viewWillAppear will be called 'back to back', or what adding the ATM view controller to a tab bar controller means in terms of these methods.
viewDidLoad
method will call only once a life time of viewController and that is when viewController object will first load in memory.
where as viewWillAppear
method will call every time when a view will appear to screen or you can say will be topViewController...
Explanation:
Consider you have tab based app with two tabs. Tab1
associated with viewController1
and tab2
is associated with viewController2
. Now when you will run your app and you will see tab one is selected and viewController1
is on view and you want to change to tab2
, when you will tap on tab2 then tabVieController2
's object will create and load to memory first time hence its viewDidLoad
method will call, and soon after that it will appear to window and viewWillAppear
will also get call.
Now if you you try changing tabs by click on them only viewWillAppear
methods will get called for both, as they are in memory already.
It simple, viewDidLoad
get called when the view is load in, either via NIB
, storyboard or with the loadView
method. The viewWillAppear:
is called when the view is presented.
When a view is added to a tab bar it only gets load once, thus the viewDidLoad
will only be called once. But if the user switch to an other tab and back to the same view the viewDidLoad
will not be called. This is because the view is already loaded.
However the viewWillAppear:
is called in both cases just before the view is shown. Thus this will be called when the user first opens the tab and when it switches back to that tab.
I think they are referring to the fact that the view is loaded every time the modal view controller appears (thus the data is constantly refreshed) but only once when it is part of tab bar (only loaded on app launch). Kind of a whacky example to explain the methods though.
You might want to read up on the view controller lifecycle to know when to implement what in which method:
Responding to Display-Related Notifications
View Loading and Unloading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With