0

I have 6 viewControllers in following manner. VCA,VCB,VCC,VCD,VCE,VCF.

mainVC-->VCA-->VCB-->VCC-->VCD...VCE

mainVC-->VCA-->VCD...VCF

(-->) are connected by segues while (...) VCE and VCF are programatically pushed in navigationController.

My question is how can I pop multiple viewControllers from VCD,VCE or VCF to VCA on Back UIBarButton on navigationController

enter image description here

According to this StackOverFlow questions Link and Link :-

Tried this code but it didn't work.

override func viewDidLoad {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = true
    let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
    self.navigationItem.leftBarButtonItem = newBackButton
}

func back(sender: UIBarButtonItem) {
   let presentingViewController = self.presentingViewController
   self.dismissViewControllerAnimated(false, completion: {
   presentingViewController!.dismissViewControllerAnimated(true, completion: {}) //.dismiss(animated:true, completion:{})
   })
}
Jay Patel
  • 2,642
  • 2
  • 18
  • 40

2 Answers2

0

If you know that VCA was pushed onto mainVC, then your navigation controller stack is [mainVC, VCA, VCB, ..., VCF] or [mainVC, VCA, VCD, ..., VCF], so you want to pop back to the VC at item 1 of the stack:

func back(sender: UIBarButtonItem) {

        if let vc = navigationController?.viewControllers[1] {
            _ = navigationController?.popToViewController(vc, animated: true)
        }

}

If what you really want to do is pop back to the root VC of the navigation controller, you can simply:

func back(sender: UIBarButtonItem) {

        _ = navigationController?.popToRootViewController(animated: true)

}

Edit:

Additional info:

// if you want to pop to the First VC, regardless of VC type
func popToFirst() {

    if let vc = navigationController?.viewControllers[1] {
        _ = navigationController?.popToViewController(vc, animated: true)
    } else {
        print("NO VC found as element 1 in navigation controller's stack")
    }

}

// if you want to pop to the First VC, but
// *only* if it is an instance of VCAViewController
func popToFirstOnlyIfA() {

    if let vc = navigationController?.viewControllers[1] as? VCAViewController {
        _ = navigationController?.popToViewController(vc, animated: true)
    } else {
        print("viewControllers[1] is NOT an instance of VCAViewController")
    }

}

// if you want to pop to an instance of VCAViewController,
// whether it's the root, first, second, third, etc VC in the stack
func popToAAnywhereInTheStack() {

    guard let aVCs = navigationController?.viewControllers else {
        print("No Array of View Controllers from navigationController!")
        return
    }

    for vc in aVCs {
        if vc is VCAViewController {
            _ = navigationController?.popToViewController(vc, animated: true)
            return
        }
    }

    print("No instance of VCAViewController found in the stack")

}

// if you simply want to pop to the navigation controller's Root VC
func popToRoot() {

    _ = navigationController?.popToRootViewController(animated: true)

}
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Thanks this worked for me. But I have a doubt, do you think that `viewController[1]` can be any other VC? Or anywhere it can go wrong? –  Jan 17 '18 at 05:27
  • Certainly, if the VC at index 1 is NOT an instance of VCA, then you would not get to VCA. See my edits for additional info, and additional ways to do it. – DonMag Jan 17 '18 at 14:45
-1

Delete all view controllers from navigationController?.viewControllers between your current view controller (it will be the last) and view controller, then do navigationController?.popViewController(animated: true)

AntiVIRUZ
  • 342
  • 1
  • 14