【问题标题】:How to programatically unwind or pop multiple viewControllers on back UIBarButton?如何以编程方式在 UIBarButton 上以编程方式展开或弹出多个视图控制器?
【发布时间】:2018-06-25 04:43:42
【问题描述】:

我有 6 个视图控制器,方式如下。 VCA,VCB,VCC,VCD,VCE,VCF.

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

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

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

我的问题是如何在BacknavigationController 上从VCDVCEVCF 弹出多个视图控制器到VCA@UIBarButton

根据这个 StackOverFlow 问题 LinkLink :-

试过这段代码,但没有用。

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

【问题讨论】:

  • 尝试实现unwind segue
  • @pacification 但我没有为VCEVCF 使用segue,unwind segue 在那里不起作用
  • mainVC 是导航控制器,VCAmainVCroot 控制器吗?还是mainVC 是root 而VCA 被推送了?
  • @Error 我的回答运气好吗?

标签: ios swift uiviewcontroller uibarbuttonitem


【解决方案1】:

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

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)

}

【讨论】:

  • 感谢这对我有用。但我有一个疑问,你认为viewController[1] 可以是任何其他VC吗?或者哪里会出错?
  • 当然,如果索引 1 处的 VC 不是 VCA 的实例,那么您将无法访问 VCA。请参阅我的编辑以获取更多信息和其他方法。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 2012-09-05
    • 2016-05-05
    • 2014-01-20
    • 2011-07-06
    • 2013-04-14
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多