Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fahied/867ff76a77d486dd98008679d7da24e0 to your computer and use it in GitHub Desktop.
Save fahied/867ff76a77d486dd98008679d7da24e0 to your computer and use it in GitHub Desktop.
Filter ViewControllers in a NavigationController by a Protocol
// Important Protocol is marked @objc and conform to AnyObject
@objc protocol Hostable: AnyObject { }
extension UINavigationController {
func viewControllers(conforming: Protocol) -> [UIViewController]? {
return viewControllers.filter { $0.conforms(to: conforming) }
}
func popToFirstViewController(conforming: Protocol, animated: Bool) {
if let destination = viewControllers(conforming: conforming)?.first {
popToViewController(destination, animated: animated)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment