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
Swift 4 UINavigationController extension version of @Maximelc's answer:
extension UINavigationController {
func popBack(_ nb: Int) {
let viewControllers: [UIViewController] = self.viewControllers
guard viewControllers.count < nb else {
self.popToViewController(viewControllers[viewControllers.count - nb], animated: true)
return
}
}
/// pop back to specific viewcontroller
func popBack(toControllerType: T.Type) {
var viewControllers: [UIViewController] = self.viewControllers
viewControllers = viewControllers.reversed()
for currentViewController in viewControllers {
if currentViewController .isKind(of: toControllerType) {
self.popToViewController(currentViewController, animated: true)
break
}
}
}
}