Skip to content

Instantly share code, notes, and snippets.

@adamgraham
Last active May 18, 2019 03:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamgraham/013d86f150845ddb6af972e23bb5131d to your computer and use it in GitHub Desktop.
Save adamgraham/013d86f150845ddb6af972e23bb5131d to your computer and use it in GitHub Desktop.
An extension of the iOS class UIView to traverse the responder chain for the nearest parent view controller.
/// An extension to traverse the responder chain for the nearest parent view controller.
extension UIView {
/// The nearest parent view controller in the responder chain.
var parentViewController: UIViewController? {
if let nextResponder = self.next as? UIViewController {
return nextResponder
} else if let nextResponder = self.next as? UIView {
return nextResponder.parentViewController
} else {
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment