Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing viewcontrollers from navigation stack

I have a navigation stack, with say 5 UIViewControllers. I want to remove the 3rd and 4th viewcontrollers in the stack on the click of a button in the 5th viewcontroller. Is it possible to do this? If so how?

like image 201
Jean Paul Scott Avatar asked Apr 23 '12 13:04

Jean Paul Scott


People also ask

How do I remove a view controller from navigation stack?

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.

What is a navigation controller for?

The navigation controller manages the navigation bar at the top of the interface and an optional toolbar at the bottom of the interface. The navigation bar is always present and is managed by the navigation controller itself, which updates the navigation bar using the content provided by its child view controllers.

How do I remove navigation controller from storyboard?

1) select navigation controller and delete connection. do same as shown in images and don't delete navigation controller at last step as i written in answer's last line. keep it disconnected as it is shown. Thanks that was it.


2 Answers

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. [navigationArray removeObjectAtIndex: 2];  // You can pass your index here self.navigationController.viewControllers = navigationArray; [navigationArray release]; 

Hope this will help you.

Edit: Swift Code

guard let navigationController = self.navigationController else { return } var navigationArray = navigationController.viewControllers // To get all UIViewController stack as Array navigationArray.remove(at: navigationArray.count - 2) // To remove previous UIViewController self.navigationController?.viewControllers = navigationArray 

Edit: To remove all ViewController except last one -> no Back Button in the upper left corner

guard let navigationController = self.navigationController else { return } var navigationArray = navigationController.viewControllers // To get all UIViewController stack as Array let temp = navigationArray.last navigationArray.removeAll() navigationArray.append(temp!) //To remove all previous UIViewController except the last one self.navigationController?.viewControllers = navigationArray 
like image 70
Nitin Avatar answered Oct 12 '22 23:10

Nitin


You can first get all the view controllers in the array and then after checking with the corresponding view controller class, you can delete the one you want.

Here is small piece of code:

NSArray* tempVCA = [self.navigationController viewControllers];  for(UIViewController *tempVC in tempVCA) {     if([tempVC isKindOfClass:[urViewControllerClass class]])     {         [tempVC removeFromParentViewController];     } } 

I think this will make your work easier.

like image 34
Sourabh Bhardwaj Avatar answered Oct 12 '22 21:10

Sourabh Bhardwaj