5

I need a way of calling the viewDidLoad method when the 'back' button is pressed in the UINavigationController. So for example if I have a flow like so:

View A -> View B -> View C

I need the it so that if I'm on View C and I press back, View B's viewDidLoad method should be called. Similarly, if I'm on View B and I press the back button View A's viewDidLoad method should be called.

Please can someone suggest how to achieve this?

James Donnelly
  • 126,410
  • 34
  • 208
  • 218

2 Answers2

10

The viewDidLoad method is called when the view controller has finished loading and setting up the view controller, for example loading the NIB file. For your needs, you should move the relevant code to viewWillAppear which will be called each time the corresponding view controller becomes visible. So for a transition A -> B -> C (backto)-> B the viewWillAppear method of B's view controller will be called twice.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • `viewDidLoad` will also be called if you have your own custom `loadVOiew` method, i.e., you're not using NIBs. And if you want the "after" event more like `viewDidLoad`, you can use `viewDidAppear`. – smparkes Jan 20 '12 at 17:53
0

From your description, it sounds like you would be better served by using the viewWillAppear or viewDidAppear methods instead. viewDidLoad is designed to only be called once, unless the view is discarded because of a memory warning.

picciano
  • 22,341
  • 9
  • 69
  • 82