Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple calls to viewDidLoad/loadView

Tags:

iphone

In one of the views I launch programmatically, I see 5 different calls to 'loadView/viewdidLoad' but I don't understand why this many are getting called. Can someone explain to me the mechanics behind this?

I launch the view in the parent UIViewController (part of a TabBar + NavigationBar application) instance in the following manner:

MainEditController *editController = [[MainEditController alloc] initWithNibName:@"MainEditView" bundle:nil];           
[self.navigationController pushViewController:editController animated:YES];         
[editController release];   

I then log MainEditController's viewDidLoad and loadView methods (and invoking their respective super methods).

The 'MainEditView' nib contains 3 items: -File's Owner (of type MainEditController), -First Responder (of type UIResponder) -View (of type UIView)

The view outlet is connected to the File's Owner and the View has no elements in it. What I intend to do is add several subs views to the main view and display one of the subviews based on a specific condition.

I thought both viewDidLoad and loadView would get called as many views (1 in this case) within the controller but that doesn't seem to be a valid assumption.

like image 465
Robin Jamieson Avatar asked Jun 11 '09 18:06

Robin Jamieson


People also ask

Can viewDidLoad be called multiple times?

viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears. In this simple app, that means it is only called once.

What is the difference between loadView and viewDidLoad?

loadView is the method that actually sets up your view (sets up all the outlets, including self. view). viewDidLoad you can figure out by its name. It's a delegate method called after the view has been loaded (all the outlets have been set) that just notifies the controller that it can now start using the outlets.

Is loadView called before viewDidLoad?

Use viewDidLoad( ), which is called AFTER loadView( ) has completed its job and the UIView is ready to be displayed. viewDidLoad( ) allows you to initialize properties of the view/viewController object and finalize them before viewWillAppear( ) is called.

What is called after viewDidLoad?

Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called. This gives you a chance to do any last-minute view setup, kick off a network request (in another class, of course), or refresh the screen.


1 Answers

Is your loadView method calling [super loadView] ? If not, the view property is likely not being set up properly, and so the next time .view is accessed,it tries to load it again.

Stab in the dark, but without the loadView method,it's hard to narrow down what might be the problem.

like image 70
Ecton Avatar answered Sep 28 '22 04:09

Ecton