2

There are a number of well known solutions for detecting when a view controller is in the process of disappearing off the screen in the process of a backwards navigation, managed by a UINavigationController (i.e. when a view controller is being popped off the navigation stack). In other words detecting a backwards navigation within the source view controller.

In contrast, I need to detect a backwards navigation within viewWillAppear for the destination view controller. Or to put it another way, detect when a view controller is about to be displayed as a result of a UINavigationController pop.

What is the cleanest/simplest way to achieve this? Implementing UINavigationControllerDelegate and setting a flag might be one approach, but would rather avoid mutable state if possible.

I am not using Storyboards to manage the transition between these two screens.

Andrew Ebling
  • 10,175
  • 10
  • 58
  • 75
  • Detecting those property `isMovingToParentViewController` or `isMovingFromParentViewController` in `viewWillAppear:` may help. – Jintao Ou Nov 07 '17 at 12:22
  • Thanks - I can confirm this works in `viewWillAppear`: `if !isMovingToParentViewController { print("backwards") }` If you post again as an answer, I will mark as accepted. – Andrew Ebling Nov 07 '17 at 12:26

1 Answers1

2

Checkout this property of UIViewController :P

isMovingToParentViewController

isMovingFromParentViewController

Jintao Ou
  • 146
  • 1
  • 7