Search code examples
swiftuiviewcontrollerunwind-segue

How to find when UIViewController is popped off the stack Swift 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?


Solution

  • 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)
       }
     }