Skip to content

Instantly share code, notes, and snippets.

@hsleedevelop
Last active February 13, 2020 11:02
Show Gist options
  • Save hsleedevelop/a1b2ada5394494b93e1873420991adb6 to your computer and use it in GitHub Desktop.
Save hsleedevelop/a1b2ada5394494b93e1873420991adb6 to your computer and use it in GitHub Desktop.
extension UIView {
func superview<T>(of type: T.Type) -> T? {
return superview as? T ?? superview.flatMap { $0.superview(of: type) }
}
}
// Example usage:
func textFieldDidBeginEditing(_ textField: UITextField) {
// Get the cell containing the textfield.
if let cell = textField.superview(of: TextFieldTableViewCell.self) {
cell.toggle(isHighlighted: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment