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 ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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,
viewDidLoadis 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 yourinitcalls.viewWillAppearis 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,viewDidUnloadorviewDidDisappear.didReceiveMemoryWarningshould 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.