Search code examples
iphoneipaduinavigationcontrollerviewdidload

How to call viewDidLoad method when back button is pressed in UINavigationController? (iPhone / iPad)


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?


Solution

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