以编程方式返回到 Swift 中连续的第 3 个前 ViewController

Programmatically go back to consecutive 3rd previous ViewController in Swift

我已经看到太多关于如何返回到以前的视图控制器的线程,这很有意义并且也很容易实现。但就我而言,只有一种并发症。考虑以下情况。

VC4 is the viewcontroller that loads up all user contacts and send information/data to that selected contact. SO far it is fine. now there is 2 conditions, if the data that I send is correctly sent then I have to go back to VC1 directly, and if the data is not sent then I have to go back to vc2 directly to take up information again.

那么处理这种情况最好的办法是什么。请建议。

更新:

I am using Tab bar controller, in that controller under one of that View , I am opening other VIew controller from this in series. Like from this Opening VC2 that opens VC3 and so on...

这就是我展示 ViewController 2

的方式
let myVC2 = self.storyboard?.instantiateViewController(withIdentifier: "idMyVC2") as MyVC2 
myVC2.modalPresentationStyle = .fullScreen 
self.present(myVC2,animated : true) 

将其插入游乐场并随意摆弄它:

import UIKit

protocol DismissDelegate: AnyObject {
    func dismissVC(_ presenting: Int)
}

class VC1: UIViewController, DismissDelegate {
    func dismissVC(_ presenting: Int) {
        guard presenting == 1 else {
            return
        }
        print("dismiss 2, 3, 4, 5")
    }
}
class VC2: UIViewController, DismissDelegate {
    weak var vc2Delegate: DismissDelegate?
    func dismissVC(_ presenting: Int) {
        guard presenting == 2 else {
            return vc2Delegate!.dismissVC(presenting)
        }
        print("dismiss 3, 4, 5")
    }
}
class VC3: UIViewController, DismissDelegate {
    weak var vc3Delegate: DismissDelegate?
    func dismissVC(_ presenting: Int) {
        guard presenting == 3 else {
            return vc3Delegate!.dismissVC(presenting)
        }
        print("dismiss 4, 5")
    }
}
class VC4: UIViewController, DismissDelegate {
    weak var vc4Delegate: DismissDelegate?
    func dismissVC(_ presenting: Int) {
        guard presenting == 4 else {
            return vc4Delegate!.dismissVC(presenting)
        }
        print("dismiss 5")
    }
}
class VC5: UIViewController {
    weak var vc5Delegate: DismissDelegate?
    func dismissStack(at presenting: Int) {
        vc5Delegate?.dismissVC(presenting)
    }
}

let vc1 = VC1()

let vc2 = VC2()
vc2.vc2Delegate = vc1

let vc3 = VC3()
vc3.vc3Delegate = vc2

let vc4 = VC4()
vc4.vc4Delegate = vc3

let vc5 = VC5()
vc5.vc5Delegate = vc4

vc5.dismissStack(at: 1) // prints: dismiss 2, 3, 4, 5
vc5.dimissStack(at: 2) // prints: dismiss 3, 4, 5

设置非常基本,每个视图控制器都通过一个委托链接起来。因为有多个视图控制器,我们创建了一个协议。该协议只是一个采用整数参数的方法,该参数是应该执行解散的视图控制器(呈现视图控制器)。然后当你想执行一个 dismiss 时,调用委托(不必来自第 5 个视图控制器),指定呈现视图控制器,委托将沿着链向下并检查它是否是呈现视图控制器(如果是,它会关闭它上面的所有内容)或不是(如果不是,它将退出一个视图控制器并重复该过程)。

VC5 有一个名为 dismissStack(at:) 的方法,它允许您输入要关闭的视图控制器的整数。因此,如果您在 2 解散,则视图控制器 3、4、5 将被解散。显然,您也可以将此方法放入其他视图控制器中。

您可以通过多种方式进行,其中一种如下所示:-

视图控制器的层次结构如下:-

NavigationController -> VC1 -> VC2 -> VC3 -> VC4(使用推送)。现在使用下面的代码更改你的控制器

如果 pop 到 root 使用:-

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

如果弹出到 VC3 只需弹出到 root。

希望对你有所帮助,

谢谢

这是我使用的一种方式

for controller in self.navigationController!.viewControllers 
{
    if let vc = controller as? LoginViewController 
    {
        self.navigationController!.popToViewController(vc, animated:    true)
              break
    }
}

我用的最多的方式是

for controller in self.navigationController.controllers {
      if let desiredVC = controller as? DesiredViewControllerName {
          self.navigationController!.popToViewController(desiredVC, animated: true)
      }
}