1

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

2 Answers 2

6

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.

2
  • viewDidLoad will also be called when the ViewController is init'ed, even if there is no XIB involved at all.
    – mmc
    Jun 30, 2009 at 21:29
  • 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. Jun 30, 2009 at 22:37
2

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).

2
  • This is incorrect—viewDidLoad is only called once (unless the view is later released to free up memory). Diedrik Hoogenboom has it right. Jun 30, 2009 at 22:35
  • 1
    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. Jun 30, 2009 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.