UIView is a subclass of UIResponder. UIResponder lays out the method nextResponder with an implementation that returns nil. UIView overrides this method, as documented in UIResponder (for some reason instead of in UIView) as follows:
if the view has a view controller, it is returned by nextResponder. If there is no view controller, the method will return the superview.Add this to your project and you're ready to roll.
代码:
@implementation UIView (Extend)
- (UIViewController *)viewController;{
id nextResponder = [self nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return nextResponder;
} else {
return nil;
}
}
@end