Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController: How do I delete a view of a stack

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?

like image 807
Thang Pham Avatar asked Apr 07 '10 18:04

Thang Pham


People also ask

How do I delete a navigation stack controller?

Use this code and enjoy: NSMutableArray *navigationArray = [[NSMutableArray alloc] initWithArray: self. navigationController. viewControllers]; // [navigationArray removeAllObjects]; // This is just for remove all view controller from navigation stack.

How do I remove Viewcontroller from memory?

You can't remove a view controller from within itself (i.e. viewDidDisappear) - what you need to do is to remove all references to it, at which point ARC will deallocate it.

Is UINavigationController a UIViewController?

A UINavigationController does a lot of this tedious work for you. As mentioned, it contains a stack of UIViewControllers. It will create a navigation bar at the top that will allow you to easily go back up the hierarchy of view controllers.


2 Answers

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.

[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:

NSArray* viewControllersInStack = self.navigationController.viewControllers;
UIViewController* targetViewController = [viewControllersInStack objectAtIndex:2];
[self.navigationController popToViewController:targetViewController animated:YES];
like image 200
Ronnie Liew Avatar answered Oct 24 '22 01:10

Ronnie Liew


Use popToViewController

[self.navigationController popToViewController:homeView animated:YES];
like image 23
Bird Avatar answered Oct 24 '22 02:10

Bird