重置 UITabBarController 上的导航堆栈不起作用
Resetting the navigation stack on a UITabBarController doesn't work
我正在尝试重置 UINavigationController
中嵌入的 UITabViewController
子类的导航堆栈,但它不起作用。
我以编程方式创建的导航堆栈是这样的:
UINavigationController => ControllerA (a subclass of UIViewController) =>
ControllerB (a subclass of UIViewController) => ControllerC (a
subclass of UITabBarController).
当用户按下 "Back" 按钮或从 ControllerC 向后滑动时,应用程序应返回到 ControllerA,而不是 ControllerB。
通常,当我想重置导航堆栈时,我会在控制器的 viewDidLoad()
方法中执行此操作:
override func viewDidLoad() {
super.viewDidLoad()
// usually work, but not in a subclass of UITabBarController as self.navigationController is nil
if let navigationController = self.navigationController {
// keep only the root controller (0) and the current controller
navigationController.viewControllers = [navigationController.viewControllers[0], self]
}
}
但这在 ControllerC(UITabViewController
的子类)中不起作用,因为 self.navigationController
为零。
如果我改为这样做(仍在 ControllerC 的 viewDidLoad()
方法中):
/// ControllerC's viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
// keep only the root controller (0) and the current controller
navigationController.viewControllers = [navigationController.viewControllers[0], self]
}
}
这可行,但是当我这样做时,ControllerB 和 ControllerC 之间没有动画:
controllerB.navigationController?.pushViewController(ControllerC(), animated: true)
我还尝试重写 ControllerC 的 viewWillDisappear()
方法:
/// ControllerC's viewWillDisappear
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParent {
if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
navigationController.popToRootViewController(animated: true)
}
}
这有效,但在显示 ControllerA 之前,ControllerB 会短暂可见。
如有任何帮助,我们将不胜感激!
在 ControllerC 中,您可以像这样覆盖 viewDidAppear()
而不是尝试覆盖 viewWillDisappear()
方法:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let navC = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
// keep only the root controller (0) and the current controller
navC.viewControllers = [navC.viewControllers[0], self]
}
}
当您向后导航时,ControllerB 不会在 ControllerA 之前短暂可见。
我正在尝试重置 UINavigationController
中嵌入的 UITabViewController
子类的导航堆栈,但它不起作用。
我以编程方式创建的导航堆栈是这样的:
UINavigationController => ControllerA (a subclass of UIViewController) => ControllerB (a subclass of UIViewController) => ControllerC (a subclass of UITabBarController).
当用户按下 "Back" 按钮或从 ControllerC 向后滑动时,应用程序应返回到 ControllerA,而不是 ControllerB。
通常,当我想重置导航堆栈时,我会在控制器的 viewDidLoad()
方法中执行此操作:
override func viewDidLoad() {
super.viewDidLoad()
// usually work, but not in a subclass of UITabBarController as self.navigationController is nil
if let navigationController = self.navigationController {
// keep only the root controller (0) and the current controller
navigationController.viewControllers = [navigationController.viewControllers[0], self]
}
}
但这在 ControllerC(UITabViewController
的子类)中不起作用,因为 self.navigationController
为零。
如果我改为这样做(仍在 ControllerC 的 viewDidLoad()
方法中):
/// ControllerC's viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
// keep only the root controller (0) and the current controller
navigationController.viewControllers = [navigationController.viewControllers[0], self]
}
}
这可行,但是当我这样做时,ControllerB 和 ControllerC 之间没有动画:
controllerB.navigationController?.pushViewController(ControllerC(), animated: true)
我还尝试重写 ControllerC 的 viewWillDisappear()
方法:
/// ControllerC's viewWillDisappear
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParent {
if let navigationController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
navigationController.popToRootViewController(animated: true)
}
}
这有效,但在显示 ControllerA 之前,ControllerB 会短暂可见。
如有任何帮助,我们将不胜感激!
在 ControllerC 中,您可以像这样覆盖 viewDidAppear()
而不是尝试覆盖 viewWillDisappear()
方法:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let navC = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
// keep only the root controller (0) and the current controller
navC.viewControllers = [navC.viewControllers[0], self]
}
}
当您向后导航时,ControllerB 不会在 ControllerA 之前短暂可见。