Mahesh Cheliya Mahesh Cheliya - 1 month ago 9
Swift Question

How can I pop specific View Controller in Swift

I used below code for

Objective-C
for pop specific view controller:

for (UIViewController *controller in self.navigationController.viewControllers) {
if ([controller isKindOfClass:[AnOldViewController class]]) {
//Do not forget to import AnOldViewController.h
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}


How can I do that in Swift?

Answer

try following code.

for controller in self.navigationController!.viewControllers as Array {
            if controller.isKindOfClass(ViewController) {
             self.navigationController?.popToViewController(controller as UIViewController, animated: true)
                break
            }
        }