UINavigationController:如何删除堆栈视图

时间:2021-12-22 09:16:50

Let say here is my stack layout

这里说的是我的堆栈布局

View3     --> Top of the stack
View2
View1
HomeView  --> Bottom of the stack

So I am in View3 now, if I click the Home button, I want to load HomeView, meaning that I need to pop View3, View2, and View1. But if I pop View3, View2 will be displayed. I dont want that. I want View3, View2, and View1 be removed, and HomeView will be displayed. Any idea how?

所以我现在在View3中,如果单击Home按钮,我想加载HomeView,这意味着我需要弹出View3,View2和View1。但如果我弹出View3,将显示View2。我不想那样。我希望删除View3,View2和View1,并显示HomeView。知道怎么样?

3 个解决方案

#1


16  

You can use popToRootViewControllerAnimated: to get to the root viewcontroller. This would pop out all the view controllers in the stack except the root view controller. In your case, this would be the HomeView.

您可以使用popToRootViewControllerAnimated:来获取根视图控制器。除了根视图控制器之外,这将弹出堆栈中的所有视图控制器。在您的情况下,这将是HomeView。

[self popToRootViewControllerAnimated:YES];


To get to a specific view in the stack, you can use popToViewController:animated: Assuming you want to pop the third viewcontroller (from bottom up). In your case, this would be view2:

要进入堆栈中的特定视图,可以使用popToViewController:animated:假设您要弹出第三个viewcontroller(从下往上)。在你的情况下,这将是view2:

NSArray* viewControllersInStack = self.navigationController.viewControllers;
UIViewController* targetViewController = [viewControllersInStack objectAtIndex:2];
[self.navigationController popToViewController:targetViewController animated:YES];

#2


2  

Use popToViewController

[self.navigationController popToViewController:homeView animated:YES];

#3


0  

use...

[self.navigationController popToRootViewControllerAnimated:YES];

#1


16  

You can use popToRootViewControllerAnimated: to get to the root viewcontroller. This would pop out all the view controllers in the stack except the root view controller. In your case, this would be the HomeView.

您可以使用popToRootViewControllerAnimated:来获取根视图控制器。除了根视图控制器之外,这将弹出堆栈中的所有视图控制器。在您的情况下,这将是HomeView。

[self popToRootViewControllerAnimated:YES];


To get to a specific view in the stack, you can use popToViewController:animated: Assuming you want to pop the third viewcontroller (from bottom up). In your case, this would be view2:

要进入堆栈中的特定视图,可以使用popToViewController:animated:假设您要弹出第三个viewcontroller(从下往上)。在你的情况下,这将是view2:

NSArray* viewControllersInStack = self.navigationController.viewControllers;
UIViewController* targetViewController = [viewControllersInStack objectAtIndex:2];
[self.navigationController popToViewController:targetViewController animated:YES];

#2


2  

Use popToViewController

[self.navigationController popToViewController:homeView animated:YES];

#3


0  

use...

[self.navigationController popToRootViewControllerAnimated:YES];