This question already has an answer here:
What is the general guideline for the difference between the types of code (object allocation, setting up ui elements, network calls, etc.) that should go in the init method vs. a viewDidLoad type method for a view controller?
Init => call at/use for initialize your ViewController and Initialize only for UIViewController
and not it's views
ViewDidLoad => call at/use for load view , This method gest called after the nib is loaded
system can unload views to save memory, it will leave the UIViewController alone. Any properties set in the init methode will not be applied again, since the UIViewController is already initialized
Initializing variables in an iOS application is something you will face every project. Choosing the right place to init your variables can sometimes be tricky. I recently faced a problem that was difficult to reproduce and changed some variables who were initialized in my viewDidLoad method and for who I couldn’t understand that they were changing.
Possible methods to init variables in your viewcontroller are: - init - viewDidLoad - viewWillAppear - viewDidAppear
To explain my point I started a new project with the template Tab Bar Application. I added logging statements to the viewDidLoad methods in both view controllers. A also added a button to the second view controller who would initialize some images that would give me a memory warning.
After logging some statements and came to the following conclusion:
15:11:40.077 testblog2[4865:707] FirstViewController viewdidload
15:11:56.720 testblog2[4865:707] SecondViewController viewDidLoad
15:12:05.710 testblog2[4865:707] Pressed loading images button
15:12:19.025 testblog2[4865:707] Received memory warning. Level=1
15:12:21.272 testblog2[4865:707] SecondViewController didReceiveMemoryWarning
=> Now changing to the first tab
15:12:30.822 testblog2[4865:707] FirstViewController viewdidload
If you initialize variables in the viewDidLoad method then please keep in mind that these variables could be reinitialized after receiving a memory warning. Cocoa will remove the view and your viewDidLoad method will be triggered again once you go to that specific view controller.
来源:https://stackoverflow.com/questions/14722300/init-method-vs-a-viewdidload-type-method