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