Skip to content

Instantly share code, notes, and snippets.

@smmcdonald
Created September 29, 2020 14:56
Show Gist options
  • Select an option

  • Save smmcdonald/50f2be5077667f54cdc44a697ab56146 to your computer and use it in GitHub Desktop.

Select an option

Save smmcdonald/50f2be5077667f54cdc44a697ab56146 to your computer and use it in GitHub Desktop.
/// Usage: Pass in the name of the UIViewController you wish to pop back to in the navigation stack. If no matching UIViewController is found, this fails silently.
/// Example: popToViewController(viewController: String(describing: StoreDetailsViewController.classForCoder()))
extension UIViewController {
func popToViewController(viewControllerNamed: String) {
if let viewControllers: [UIViewController] = self.navigationController?.viewControllers {
for view in viewControllers {
if (String(describing: view.classForCoder) == viewControllerNamed) {
self.navigationController?.popToViewController(view, animated: true)
break
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment