About viewController's viewDidLoad and viewWillAppear methods
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
andviewDidLoad
, 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".
UIViewController viewDidLoad vs. viewWillAppear: What is the proper division of labor?
viewDidLoad is things you have to do once. viewWillAppear gets called every time the view appears. You should do things that you only have to do once in viewDidLoad - like setting your UILabel texts. However, you may want to modify a specific part of the view every time the user gets to view it, e.g. the iPod application scrolls the lyrics back to the top every time you go to the "Now Playing" view.
However, when you are loading things from a server, you also have to think about latency. If you pack all of your network communication into viewDidLoad or viewWillAppear, they will be executed before the user gets to see the view - possibly resulting a short freeze of your app. It may be good idea to first show the user an unpopulated view with an activity indicator of some sort. When you are done with your networking, which may take a second or two (or may even fail - who knows?), you can populate the view with your data. Good examples on how this could be done can be seen in various twitter clients. For example, when you view the author detail page in Twitterrific, the view only says "Loading..." until the network queries have completed.
viewDidLoad vs ViewWillAppear in IOS
viewDidLoad
is not called once for the Application. It is get called once for that viewController when the view holds memory and loaded.
So as many number of of time you push to the viewController, that many times it will call the viewDidLoad
viewDidLoad()
— Called when the view controller’s content view (the top
of its view hierarchy) is created and loadedviewWillAppear()
— Intended for any operations that you want always to
occur before the view becomes visible.
For more info about this look at the link : https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html
So if the view is already in memory (Like your case), then no need to push again, only need to pop back by this code
self.navigationController?.popViewControllerAnimated(true)
What is the difference between -viewWillAppear: and -viewDidAppear:?
In general, this is what I do:
ViewDidLoad - Whenever I'm adding controls to a view that should appear together with the view, right away, I put it in the
ViewDidLoad
method. Basically, this method is called whenever the view was loaded into memory. So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without these forms.ViewWillAppear: I use
ViewWillAppear
usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation ofUIViews
is fairly expensive, and you should avoid as much as possible doing that on theViewWillAppear
method because when this gets called, it means that the iPhone is already ready to show theUIView
to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).ViewDidAppear: Finally, I'm using
ViewDidAppear
to start off new threads to things that would take a long time to execute, like for example doing a web service call to get extra data for the form above. The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data.
What is better place for loading data from API- viewDidLoad, viewWillAppear or viewDidAppear?
viewDidLoad
method is called first time when UIViewController
is first loaded and when it pop and then you reenter in it at that time viewDidLoad
is called. So if you want to load the API only once then viewDidLoad
is the best place to call an API.
viewWillAppear
called every time when you enter in that UIViewController
and it is the place load the API when you want to get refreshed data (updated data).
viewDidAppear
also called like viewWillAppear
but bit late called than viewWillAppear
so if you want to call the API every time than the best place is viewWillAppear
method.
Because viewDidAppear
method called late from viewWillAppear
method and you are just requesting the API so the response of API may be late and If your UI change based on API response then it will stuck the application UI so there is a best place to call API either viewDidLoad
& viewWillAppear
methods.
Looking to understand the iOS UIViewController lifecycle
All these commands are called automatically at the appropriate times by iOS when you load/present/hide the view controller. It's important to note that these methods are attached to UIViewController
and not to UIView
s themselves. You won't get any of these features just using a UIView
.
There's great documentation on Apple's site here. Putting in simply though:
ViewDidLoad
- Called when you create the class and load from xib. Great for initial setup and one-time-only work.ViewWillAppear
- Called right before your view appears, good for hiding/showing fields or any operations that you want to happen every time before the view is visible. Because you might be going back and forth between views, this will be called every time your view is about to appear on the screen.ViewDidAppear
- Called after the view appears - great place to start an animations or the loading of external data from an API.ViewWillDisappear
/DidDisappear
- Same idea asViewWillAppear
/ViewDidAppear
.ViewDidUnload
/ViewDidDispose
- In Objective-C, this is where you do your clean-up and release of stuff, but this is handled automatically so not much you really need to do here.
May I use viewWillAppear instead of viewDidLoad for most of my initialization and setup?
It depends on what you intend to do after the form start-to-finish of loading the view.
You don't want to do too much in viewWillAppear
(Called when the view is ready to be shown) as it could impact performance; typically you want to do things like refresh a table, or update the text on a label etc. The viewDidLoad
method is called once the view is loaded and it is common to add things like buttons, labels etc, anything that you want to appear on the view. If you have anything tasks that may take long to execute, it is better to do them in the viewDidAppear
as the view has already been loaded; it is good practice to perform these methods on a separate thread, or at least provide the user with some sort of activity indicator until the work is done.
iOS 7 - Difference between viewDidLoad and viewDidAppear
Please Follow the below View Controller Life Cycle Every Time. You will be amazed with the coding and performance of your application in this manner.
viewDidLoad and viewWillAppear with tab bar controller
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.
Related Topics
Symbolicate Crash in iOS8 with Xcode 6 .1
Where Is the Official Documentation for Cvopenglestexture Method Types
Titletextattributes Uiappearance Font in iOS 7
How to Debug an Issue with a Release Mode Build in iOS
How to Get the MAC Os X Firewall to Permanently Allow My iOS App
How Do Set a Width and Height of an Image in Swift
How to Implement Localization in Swift Ui
Less Blur with 'Visual Effect View with Blur'
What Is Container View in iOS 5 Sdk
iOS Enterprise Provisioning Profile Expiration
Add Uiview Banner Above Status Bar iOS 7
Pulling Data from a Cmsamplebuffer in Order to Create a Deep Copy
Debugging Mobile Safari in iOS 8 and iOS 9
A Server with the Specified Hostname Could Not Be Found
Ios: Avplayer - Getting a Snapshot of the Current Frame of a Video