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?
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With