Brant
Brant

Reputation: 15344

In what order are view methods called when transitioning from one view controller to another?

When I transition from one view controller (let's call it MasterViewController) to another (called DetailViewController), in what order are the viewWillDisappear:, viewWillAppear:, etc. methods on each controller called?

I suspect some of the cleanup code called when my master view disappears is interfering with the initialization code in my detail view. I've looked through Apple's documentation but can't find any information involving multiple view controllers like this.

Upvotes: 3

Views: 1740

Answers (1)

Brant
Brant

Reputation: 15344

I created a simple UINavigationController-based project and added some NSLog statements to find out what order they get called in.

  1. Master prepareForSegue:
  2. Detail viewDidLoad
  3. Master viewWillDisappear:
  4. Detail viewWillAppear:
  5. The new view is displayed (with or without animation)
  6. Master viewDidDisappear: (after animation is finished)
  7. Detail viewDidAppear:

However, when switching between views using a UITabViewController, the order is different:

  1. SecondTab viewDidLoad
  2. SecondTab viewWillAppear:
  3. FirstTab viewWillDisappear:
  4. The new view is displayed.
  5. FirstTab viewDidDisappear:
  6. SecondTab viewDidAppear:

So it seems that you cannot always count on these events occuring in the same order - it can vary depending on the nature of the view controllers you are transitioning between.

Are there any important points that I missed here?

Upvotes: 9

Related Questions