Last active
May 18, 2019 03:12
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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