ALOK NAIR thoughts.toString( )

The Inconsistent Order of View Transition Events

You should never write code depending on any particular order of

  • viewWillAppear:
  • viewDidAppear:
  • viewWillDisappear:
  • viewDidDisappear:

calls in any app with more that one kind of view transitions because each kind of view transition has its own order and it may be incompatible with the assumption of your code.

For demonstration I’ll show you the cases of three common view transitions.

  • Navigation

    Normal: (Pushing and popping, interactive or not)

    1. CurrentviewWillDisappear
    2. NewviewWillAppear
    3. CurrentviewDidDisappear
    4. NewviewDidAppear

    Canceled Interactive Popping:

    1. CurrentviewWillDisappear
    2. NewviewWillAppear
    3. NewviewWillDisappear
    4. NewviewDidDisappear
    5. CurrentviewWillAppear
    6. CurrentviewDidAppear
  • Page Scrolling (UIPageViewControllerTransitionStyleScroll)

    Normal:

    1. NewviewWillAppear
    2. CurrentviewWillDisappear
    3. NewviewDidAppear
    4. CurrentviewDidDisappear

    Canceled Interactive Scrolling:

    1. NewviewWillAppear
    2. CurrentviewWillDisappear
    3. CurrentviewWillAppear
    4. CurrentviewDidAppear
    5. NewviewWillDisappear
    6. NewviewDidDisappear
  • Page Curling (UIPageViewControllerTransitionStylePageCurl)

    Normal:

    1. NewviewWillAppear
    2. CurrentviewWillDisappear
    3. CurrentviewDidDisappear
    4. NewviewDidAppear

    Canceled Interactive Curling:

    1. NewviewWillAppear
    2. CurrentviewWillDisappear
    3. NewviewWillDisappear
    4. CurrentviewWillAppear
    5. NewviewDidDisappear
    6. CurrentviewDidAppear

As you can see:

  • New view’sviewWillAppearmay be called before or after current view’sviewWillDisappear.
  • New view’sviewDidAppearmay be called before or after current view’sviewDidDisappear.
  • For interactive canceling, new view’sviewDidDisappearis not balanced withviewDidAppear— there is noviewDidAppearbetweenviewWillAppearandviewWillDisappear.
  • Of course, for the same view,viewWillAppearis always beforeviewDidAppearandviewWillDisappearis always beforeviewDidDisappear, if only both are gotten called — see point 3.
  • The only certain order between current view and new view is that current view’sviewWillDisappearis before new view’sviewDidAppearand new view’sviewWillAppearis before current view’sviewDidDisappear, but if only both are gotten called — see point 3.
comments powered by Disqus