arm1.ru

UIViewController Lifecycle

UIViewController Lifecycle

A small cheat sheet on the UIViewController lifecycle - which methods are called, and in what order, during different transitions between UIViewControllers.

Controller A Pushes to Controller B

Call order during the transition:

  1. initWithCoder:
    1. When a storyboard is used, this method is called instead of init or initWithNibName:bundle:
  2. awakeFromNib
  3. willMoveToParentViewController:
  4. prefersStatusBarHidden
  5. preferredStatusBarUpdateAnimation
  6. loadView
    1. loadView assigns all outlets (IBOutlets) to the corresponding @property.
  7. prepareForSegue:sender:
  8. viewDidLoad
    1. All outlets are already connected, but the views have not been rendered yet
  9. extendedLayoutIncludesOpaqueBars
  10. edgesForExtendedLayout
  11. viewWillAppear:
  12. extendedLayoutIncludesOpaqueBars
  13. edgesForExtendedLayout
  14. updateViewConstraints
  15. viewWillLayoutSubviews
  16. viewDidLayoutSubviews
  17. Animation
  18. viewDidAppear:
  19. didMoveToParentViewController:
  20. updateViewConstraints
  21. viewWillLayoutSubviews
  22. viewDidLayoutSubviews

Some of the methods are called more than once. Layout is called twice - before and after the animation.

Controller B Pushes to Controller C

During the transition:

  1. prepareForSegue:sender:
  2. viewWillDisappear:
  3. updateViewConstraints
  4. Animation
  5. viewDidDisappear:

Controller C Pops to Controller B

When returning from C to B, this happens:

  1. prefersStatusBarHidden
  2. preferredStatusBarUpdateAnimation
  3. extendedLayoutIncludesOpaqueBars
  4. edgesForExtendedLayout
  5. viewWillAppear:
  6. extendedLayoutIncludesOpaqueBars
  7. edgesForExtendedLayout
  8. updateViewConstraints
  9. viewWillLayoutSubviews
  10. viewDidLayoutSubviews
  11. Animation
  12. viewDidAppear:
  13. updateViewConstraints
  14. viewWillLayoutSubviews
  15. viewDidLayoutSubviews

Controller B Pops to Controller A

Leaving B for good:

  1. willMoveToParentViewController:
    1. The argument for this call is nil, which means that scene B will be removed from the hierarchy.
  2. viewWillDisappear:
  3. updateViewConstraints
  4. viewDidDisappear:
  5. Animation
  6. didMoveToParentViewController:
    1. The argument is also nil
  7. dealloc

Source.

keyboard_return