3

On a UINavigationController I have two view controllers 1 and 2. There is a Show segue from 1 to 2 and one programmatic unwind segue (control+drag from the scene's view controller icon to its exit icon) from 2 to 1.

How can I know when the 2 is just about to be popped of the UINavigationController stack?

viewWillDisappear, is also called when another view is pushed onto the UINavigationController stack so it's not a solution. I've seen an answer on SO suggesting to create back button rather than making use of the default back button, but I am wondering is there no system method I can use to find out when a UIViewController is popped off the stack?

Community
  • 1
  • 1
bibscy
  • 2,598
  • 4
  • 34
  • 82
  • Why do you need to know exactly. Can you not trigger something in viewwillappear somewhere else? Just trying to get context – agibson007 Mar 14 '17 at 02:23
  • @agibson007 I tried to make the scenario simpler, but my case is: I have 9 UIViewControllers on a UINavigationController. In 4th if user is loggedIn, segue to 8th,else segue to Fifth. In 8th I have a tableView, if row1 is touched segue to 9th, else segue to 10th. In 10th when I touch back button(view is about to disappear) I need to know if I should segue to 9th or to 8th based on the row that was previously selected in 8th. `viewWillDisapper` is called both when UINavigationController is pushed or popped so using it in conjunction with `isMovingFromParentViewController` works for my case. – bibscy Mar 14 '17 at 02:36

1 Answers1

4

I think I might have found the answer viewWillDisappear should be use together with isMovingFromParentViewController. Please feel free to correct me if this approach is prone to bugs.

override func viewWillDisappear(_ animated : Bool) {
    super.viewWillDisappear(animated)

   if self.isMovingFromParentViewController{
        self.performSegue(withIdentifier: "fromEighthToFourth", sender: self)
   }
 }
bibscy
  • 2,598
  • 4
  • 34
  • 82
  • That's the correct technique to detect that you're being popped, but I wouldn't use it to perform a segue if I were you, because you are in the middle a segue already. – matt Mar 14 '17 at 02:25
  • @matt What's the best alternative to call the unwind segue earlier? – bibscy Mar 14 '17 at 02:37