Where to put DetailViewController init code (no IB)?


#1

Awesome book. Thanks for choosing to start with “Window-based Application” template most of the time. :slight_smile:

I have implemented ItemDetailViewController’s view textfields and labels programmatically, without using IB. My question is: what is the most appropriate place to put the widgets init code, i.e. things like

Currently, I have all of them in -(id)init{…} (including also cameraButton, imagePickerController and imagePickerPopover), but it also works if I put them in -(void)loadView{…}, and possibly even in -(void)viewWillAppear, although I have not tried the last one. What is a better programming practice?

Also, possibly related question, do I need to release those widgets in viewDidUnload (like on pg 176) if I create them programmatically as in the above snippet?


#2

Your views should always be created in loadView. If you create them in init, it works - until you get a low memory warning. Make sure to re-read the original chapter on view controllers and then the chapter on Low Memory Warnings.


#3

Thanks for your reply, that makes sense. In fact, loadView was my first choice, but then I got a little confused because I saw you create the cameraButton in init (why?), which prompted me to put all the rest of the detail view creation there and see what happens :slight_smile:

By the way, if the code is (incorrectly) placed in init, low memory warning simulation does not break the app. In particular, simulating low memory while in ItemsViewController and then going into detail view, the latter looks ok.