Which is the best place to initialize the data structures used to display content in my view controllers: init
or viewDidLoad
?
Also, for either case, where should I be releasing them: dealloc
, viewDidUnload
or didReceiveMemoryWarning
?
The all-encompasing answer is, "It depends upon your view lifecycle." Each method has its own place relative to how your views will load and appear.
As a rule of thumb, though, viewDidLoad
is a good default place to go first - your XIB views will already be instantiated so you can set their properties. If you do a lot of loading and unloading of views, you might want to push pure data that's fairly static back to your init
calls.
viewWillAppear
is another option, depending again upon your views' life cycles and how much your data is likely to change while the view is hidden.
ETA: Release your memory in the complementary calls to where you allocate it. In dealloc
, viewDidUnload
or viewDidDisappear
. didReceiveMemoryWarning
should always unload anything you can regardless of where you initialize it, and your code should be smart enough to reload the data once it is active again.
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