4

Let's say I have five view controllers and I want to go to the specific view controller

RootViewController ==> FirstViewController ==> SecondViewController ==> ThirdViewController ==> FourthViewController(Modally presented having a button) and all other controllers I connected through push method. My task is I want to go to the firstViewController from FourthViewController when button is clicked. Any help?

        for controller in self.navigationController!.viewControllers as Array {
        if controller.isKind(of: HomeViewController.self) {
            self.navigationController!.popToViewController(controller, animated: true)
            break
        }
    }

this is the code I have done.

Wings
  • 2,398
  • 23
  • 46
  • use poptoviewcontroller and specify the viewcontroller if you are ising navigationcontroller – Vinodh Mar 14 '18 at 15:03
  • check this https://stackoverflow.com/questions/49050782/how-to-show-a-specific-vc-more-like-navigate-to-a-stack-of-vc/49066910#49066910 – Reinier Melian Mar 14 '18 at 15:07

2 Answers2

6

Add delegate in FourthViewController:

self.dismiss(animated: true) {
    self.delegate.popToFirstVC()
}

Add func popToFirstVC() in ThirdViewController. Use popToViewController:

func popToFirstVC() {
    if let firstViewController = self.navigationController?.viewControllers[1] {
        self.navigationController?.popToViewController(firstViewController, animated: true)
    }
}

or better

guard let viewControllers = self.navigationController?.viewControllers else {
    return
}

for firstViewController in viewControllers {
    if firstViewController is FirstViewController {
        self.navigationController?.popToViewController(firstViewController, animated: true)
        break
    }
}

There is still such an option. Add an Observer for this function and call where necessary. But I would do it only in the most extreme cases.

func popToThisVC() {
    if let topController = UIApplication.topViewController() {
        topController.navigationController?.popToViewController(self, animated: true)
    }
}
maxwell
  • 3,788
  • 6
  • 26
  • 40
  • what is 1 in viewControllers[1] ? – Wings Mar 14 '18 at 15:11
  • Are all your controllers in Navigation stack? – maxwell Mar 14 '18 at 15:23
  • Yes all my navigation controllers are in stack – Wings Mar 14 '18 at 15:24
  • Do you have any method which uses protocols to go back to specific view controller because it always works on my app? – Wings Mar 14 '18 at 15:37
  • I think that using the protocol is good if you need to go back to the previous VC. If there are many controllers, then it is necessary to transfer the whole chain from the first VC to the last VC, which seems to me not very good. – maxwell Mar 14 '18 at 15:41
  • but all of these is not working is not working in modal presented view controller. These methods are working fine only in push method. – Wings Mar 14 '18 at 15:45
  • Thanks but I will try tomorrow because my company is closed :) – Wings Mar 14 '18 at 16:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166872/discussion-between-v-rohit-and-maxwell). – Wings Mar 15 '18 at 07:41
0

You mentioned connected, hence I reckon you used storyboards and segues, in that case why not create an unwind segue? It's a bit hard to show you snippet of unwind segue here via text only but I think Unwind segue blog from medium has your answer

Happiehappie
  • 1,084
  • 2
  • 13
  • 26