Skip to content

Instantly share code, notes, and snippets.

@TiagoBras
Created April 18, 2019 11:23
Show Gist options
  • Select an option

  • Save TiagoBras/ca7ce9c234ac8b9f2b510afd3efbfa19 to your computer and use it in GitHub Desktop.

Select an option

Save TiagoBras/ca7ce9c234ac8b9f2b510afd3efbfa19 to your computer and use it in GitHub Desktop.
Easily pop to a view controller
import UIKit
extension UINavigationController {
/// Pops to the nearest view controller of type *viewControllerType*.
///
/// - Parameter viewControllerType: UIViewController.Type
/// - Returns: true if it successfully popped to a view controller of the type passed.
@discardableResult func popToViewController(ofType viewControllerType: UIViewController.Type) -> Bool {
if let vc = viewControllers.last(where: { type(of: $0) == viewControllerType }) {
popToViewController(vc, animated: true)
return true
} else {
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment