Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View controller lifecycle when swiping-to-pop from UINavigationController in iOS7

What is the impact of iOS 7's new swipe-to-pop gesture on the view controller lifecycle of UINavigationController?

like image 970
steveluscher Avatar asked Sep 21 '13 01:09

steveluscher


People also ask

How do I pop a view controller in Swift?

You can do it by selecting the View Controller in Storyboard editor and clicking Editor -> Embed In -> Navigation Controller. Also make sure that you have your Storyboard Entry Point (the arrow that indicates which view controller is presented first) either pointing to Navigation Controller or before it.

Which method is used to display the view controller in the UINavigationController container in Swift?

You add and remove view controllers from the stack using segues or using the methods of this class. The user can also remove the topmost view controller using the back button in the navigation bar or using a left-edge swipe gesture.


1 Answers

New in iOS 7 is a swipe-to-pop gesture in UINavigationController; you can swipe your finger from left-to-right to do what is normally done with the back button. The thing you have to watch for, is that the pop transition is cancellable.

Tapping the back button still works the way you would expect it to:

  1. User taps the back button
  2. viewWillDisappear is called
  3. viewDidDisappear is called
  4. Popped controller is released

Completing a swipe-to-pop is like tapping the back button

  1. User begins to swipe from left-to-right
  2. viewWillDisappear is called
  3. User completes the swipe gesture
  4. viewDidDisappear is called
  5. Popped controller is released

A cancelled swipe-to-pop works like this

  1. User begins to swipe from left-to-right
  2. viewWillDisappear is called
  3. User aborts the swipe gesture (by reversing it, or by not dragging far enough to the right)
  4. viewWillAppear is called
  5. viewDidAppear is called

Update your expectations, and your view lifecycle methods accordingly.

Of note is the fact that viewWillDisappear is called in all cases, even if the user aborts the pop gesture. This may prove be a lie; if the swipe-to-pop gesture is aborted, your view isn't going to disappear, is it?

Like all of us, I strive to balance addObserver/removeObserver calls in my app to prevent crashes. I override the view controller lifecycle methods viewWillAppear/viewWillDisappear to do the housekeeping, and maintain a setupPerformed flag to prevent observers from being added more than once during a view controller's lifetime. Moreover, I have relied on this trick to determine whether a view controller is about to be popped off a navigation controller's stack and subsequently released, so that I can know when precisely to tear down my observers.

iOS 7 has complicated this pattern. I can no longer rely on a view controller being torn down when viewWillDisappear is called. I have to expect that it might turn around, yell "just kidding," and proceed to call viewWillAppear/viewDidAppear all over again.

like image 79
steveluscher Avatar answered Oct 19 '22 11:10

steveluscher