I am using navigation based application. I push First ViewController to Second ViewController and from Second ViewController to Third ViewController. Now I want to pop from
Often it is more important to do that from top of stack, so:
- (void)popToLast:(Class)aClass
{
for (int i=self.navigationController.viewControllers.count-1; i>=0; i--)
{
UIViewController *vc = self.navigationController.viewControllers[i];
if ([vc isKindOfClass:aClass])
{
[self.navigationController popToViewController:vc animated:YES];
break;
}
}
}
and you call that
popToLast:[SomeViewController class];
After lots of effort someone has created swift extension of back to a particular view controller in Swift 3.0.
extension UINavigationController {
func backToViewController(viewController: Swift.AnyClass) {
for element in viewControllers as Array {
if element.isKind(of: viewController) {
self.popToViewController(element, animated: true)
break
}
}
}
}
Method calling:
self.navigationController?.backToViewController(viewController: YourViewController.self)
i have answer here. This is 100% working code for Swift > 4.X
How can I pop specific View Controller in Swift
Updated for Swift 3:
used below simple code, for pop to specific view controller;
for vc in self.navigationController!.viewControllers as Array {
if vc.isKind(of: YourViewControllerName) {
self.navigationController!.popToViewController(vc, animated: true)
break
}
}