1
votes

Let's say I have four VCs in a stack: View1, View2, View3, and View4. Upon finishing with View4, I want to go back to View2 instead of popping to View3.

The code I currently use to accomplish this is:

let allViewControllers = self.navigationController?.viewControllers
       for thisVC in allViewControllers!{ 
           if thisVC.isKind(of: View2.self){
               self.navigationController?.popToViewController(thisVC, animated: true)
           }
       }

The problem that I'm experiencing with this is I see the View3 animate by before eventually landing on View2. In other words, I press the button on View4 which calls the aforementioned code, then it pops to View3 briefly before then popping to View2. Is there any way to jump directly to View2 without seeing View3 in between?

In case it's relevant, here is the code I use to push View4 from View3

let vc = self.storyboard!.instantiateViewController(withIdentifier: "View3") as! View3ViewController
self.navigationController?.pushViewController(vc, animated: true)
1
Sorry, but I cannot reproduce behavior described, neither on a device nor in the simulator (even with animation debugging). I do not see the ViewControllers in-between, the animation directly goes to the View2 controller. - Andreas Oetjen
Hmm, that's bizarre. Perhaps I'm doing something wrong when pushing View4 from View3. I'll edit my question to include that code. - user1328035

1 Answers

0
votes

You can simply achieve this by using storyboard identifiers of your view controllers.

let mainStoryBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let view2 : UIViewController = mainStoryBoard.instantiateViewController(withIdentifier: "view_2_id") as UIViewController
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = view2
        self.window?.makeKeyAndVisible()