我正在寻找适合这种情况的字词,方法或类似内容。假设我有3个视图控制器,a,b和c。
然后我从a导航到b,最后从b导航到c。从c开始,我要一直撤消我的视图控制器,直到a。我如何以编程方式实现这一目标?
答案 0 :(得分:2)
如果您使用的是UINavigationController,则可以使用popToRootViewController
import UIKit
class AViewController: UIViewController {}
class BViewController: UIViewController {}
class CViewController: UIViewController {}
let a = AViewController()
let b = BViewController()
let c = CViewController()
let nav = UINavigationController(rootViewController: a)
nav.pushViewController(b, animated: true)
nav.pushViewController(c, animated: true)
// Option 1
nav.popToRootViewController(animated: true)
// Option 2
nav.popToViewController(a, animated: true)
答案 1 :(得分:1)
最简单的方法是使用UINavigationController
:
从A-> B(此代码在A中):
navigationController?.pushViewController(ViewControllerB(), animated: true)
从B-> C(B中的此代码):
navigationController?.pushViewController(ViewControllerC(), animated: true)
从C-> A(此代码在C中):
navigatinoController?.popToRootViewController(animated: true)