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:
- User taps the back button
viewWillDisappear
is calledviewDidDisappear
is called- Popped controller is released
Completing a swipe-to-pop is like tapping the back button
- User begins to swipe from left-to-right
viewWillDisappear
is called- User completes the swipe gesture
viewDidDisappear
is called- Popped controller is released
A cancelled swipe-to-pop works like this
- User begins to swipe from left-to-right
viewWillDisappear
is called- User aborts the swipe gesture (by reversing it, or by not dragging far enough to the right)
viewWillAppear
is calledviewDidAppear
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.