I have found many ways to pop back 2 UIViewControllers
in UINavigationController
using Objective-C, however when I try and switch that over to Swift it
let allViewControllers : [UIViewController] = self.navigationController!.viewControllers as [UIViewController]
self.navigationController?.popToViewController(allViewControllers[allViewControllers.count-3], animated: true)
Here is another, slightly "fool-proof" version:
extension UINavigationController {
func popBack(_ count: Int) {
guard count > 0 else {
return assertionFailure("Count can not be a negative value.")
}
let index = viewControllers.count - count - 1
guard index > 0 else {
return assertionFailure("Not enough View Controllers on the navigation stack.")
}
popToViewController(viewControllers[index], animated: true)
}
}