在视图控制器之间快速导航

问题描述 投票:0回答:2

我正在寻找适合这种情况的字词,方法或类似内容。假设我有3个视图控制器,a,b和c。

然后我从a导航到b,最后从b导航到c。从c开始,我要一直撤消我的视图控制器,直到a。如何以编程方式实现此目标?

objective-c swift viewcontroller programmatically
2个回答
0
投票

如果使用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)

0
投票

最简单的方法是使用UINavigationController:从A-> B(此代码在A中):

navigationController?.pushViewController(ViewControllerB(), animated: true)

从B-> C(B中的此代码):

navigationController?.pushViewController(ViewControllerC(), animated: true)

从C-> A(此代码在C中:):

navigatinoController?.popToRootViewController(animated: true)
© www.soinside.com 2019 - 2024. All rights reserved.