views:

3407

answers:

2

Hi all,

Some times when I push ViewController into Navigation Controller, the viewDidLoad() method of the View Controller is not called. And this cause my application to crash. I would appreciate any help.

I forget to mention that I load the view from the nib before I push it to the Navigation Controller.

Thanks, Sarah

+4  A: 

The viewDidLoad method is only called when the view is first loaded from the Nib file. If the view was already loaded and you push the view again it will not fire again. Depending on what you want to do, you may want to use viewWillAppear or viewDidAppear instead.

Diederik Hoogenboom
viewDidLoad will also be called when the ViewController is init'ed, even if there is no XIB involved at all.
mmc
viewDidLoad is called immediately after loadView, which is called the first time the .view property is accessed. The default implementation of loadView loads a nib file (if you provided a nib file name when you inited the view controller), but you can override loadView to create the view in code.
Brent Royal-Gordon
+2  A: 

Once the View is loaded and added to the Controller's stack, you will not see this called again. You would need the view to get popped off the stack and pushed again to see it. You can always be assured viewWillAppear will get invoked everytime you return to the view. This allows you to do any housekeeping that may be in order (which i assume is the goal).

MystikSpiral
This is incorrect—viewDidLoad is only called once (unless the view is later released to free up memory). Diedrik Hoogenboom has it right.
Brent Royal-Gordon
Which is exactly what I wrote. I said it is called once and you need to implement viewWillAppear to do things when you cycle back to the view. Please read the post.
MystikSpiral