Swift 在视图控制器之间导航
Swift navigating between viewcontrollers
我正在寻找适合这种情况的词、方法或类似词。假设我有 3 个视图控制器,a、b 和 c。
然后我从 a 导航到 b,最后从 b 导航到 c。从 c 然后我想关闭我的视图控制器一直回到 a。我如何以编程方式实现此目的?
如果您使用的是 UINavigationController you can use 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)
最简单的方法是使用 UINavigationController
:
从 A -> B(A 中的代码):
navigationController?.pushViewController(ViewControllerB(), animated: true)
从 B -> C(B 中的代码):
navigationController?.pushViewController(ViewControllerC(), animated: true)
从 C -> A(此代码在 C 中):
navigatinoController?.popToRootViewController(animated: true)
我正在寻找适合这种情况的词、方法或类似词。假设我有 3 个视图控制器,a、b 和 c。
然后我从 a 导航到 b,最后从 b 导航到 c。从 c 然后我想关闭我的视图控制器一直回到 a。我如何以编程方式实现此目的?
如果您使用的是 UINavigationController you can use 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)
最简单的方法是使用 UINavigationController
:
从 A -> B(A 中的代码):
navigationController?.pushViewController(ViewControllerB(), animated: true)
从 B -> C(B 中的代码):
navigationController?.pushViewController(ViewControllerC(), animated: true)
从 C -> A(此代码在 C 中):
navigatinoController?.popToRootViewController(animated: true)