The docs say that the whole subview hierarchy can be created in -loadView. But there's also this -viewDidLoad method which sounds nice to ovewrite for actually building the hierarchy, when the view loaded. I guess it's a matter of taste only. But maybe doing so in -viewDidLoad hast the advantage that the view controller already adjusted the frame of the view correctly to accomodate for the status bar or any other kind of bar like tab bar or tool bar?
views:
35answers:
2I think the logic behind it is that in loadView you build elements, i.e. instantiate and allocate memory, this being the most costly action. So when [super loadView]
has been called all the way up the ladder, all views are ready to be displayed and the expensive part is over.
In - (void) viewDidLoad
you populate the views with data, viewDidLoad "know" all elements are ready to have text set on them, values, content etc.
UINavigationControllers etc. can benefit from knowing when everything is ready as to start animating view on to the screen. If you place all the instantiation/allocation in the viewDidLoad you force the controller to do everything, including being animated on screen, at the last moment. I don't know the exact details, but this is what I get from the documentation and the "life-cycle" of the UIViewController
I usually do it like this:
- (void)loadView {
[super loadView];
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 320.0f, 460.0f)];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg02.png"]];
[container addSubview:imageView];
[imageView release];
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_search.png"]];
[container addSubview:background];
[background release];
UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 200.0f, 20.0f)];
[self setLabel:textlabel];
[container addSubview:self.label];
[textlabel release];
[self setView:container];
[container release];
}
- (void)viewDidLoad {
[self.label setText:@"I am ready!"];
[super viewDidLoad];
}
I sometimes use the viewWillAppear to set data, if it is a view controller that sports modalViewControllers or pushes a new viewController where a user sets data that needs to be updated on the parent when popping the view again (e.g. a UITableViewController with a list of songs is pushed, the user selects a song and the controllers pops and the parent now displays the song title the user selected etc.) Any case wher the view goes on/off screen often, but needs to display newly changed data.
viewDidLoad is called after the view hierarchy is built. loadView is supposed to build the hierarchy and set the view property if you are not loading from a NIB file.
viewDidLoad is also called after a memory warning if you're view got unloaded. When you get a memory warning viewDidUnload is called to give you a change to release objects that can be easily recreated in viewDidLoad.
Read the "Subclassing Notes" and "Memory Management" sections of the class description to better understand how it works.