如何以编程方式在后面的 UIBarButton 上展开或弹出多个 viewController?

How to programatically unwind or pop multiple viewControllers on back UIBarButton?

我按照以下方式有 6 个 viewController。 VCA,VCB,VCC,VCD,VCE,VCF.

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

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

(-->) 通过 segues 连接,而 (...) VCEVCF 以编程方式连接 pushed in navigationController.

我的问题是如何从 VCDVCEVCF 弹出多个 viewControllers 到 Back 上的 VCA UIBarButton navigationController

根据这个 Whosebug 问题 and Link :-

试过此代码但没有用。

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:{})
   })
}

navigationController?.viewControllers 中删除当前视图控制器(这将是最后一个)和视图控制器之间的所有视图控制器,然后执行 navigationController?.popViewController(animated: true)

如果您知道 VCA 被推到 mainVC,那么您的导航控制器堆栈是 [mainVC, VCA, VCB, ..., VCF][mainVC, VCA, VCD, ..., VCF],因此您想弹回到 VC 在堆栈的第 1 项:

func back(sender: UIBarButtonItem) {

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

}

如果你真正想要做的是弹出回导航控制器的root VC,你可以简单地:

func back(sender: UIBarButtonItem) {

        _ = navigationController?.popToRootViewController(animated: true)

}

编辑:

附加信息:

// 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)

}