I have a pointer to a UIView. How do I access its UIViewController? [self superview] is another UIView, but not the
The fast and generic way in Swift 3:
extension UIResponder {
func parentController(of type: T.Type) -> T? {
guard let next = self.next else {
return nil
}
return (next as? T) ?? next.parentController(of: T.self)
}
}
//Use:
class MyView: UIView {
...
let parentController = self.parentController(of: MyViewController.self)
}