获取当前View所在的ViewController

获取当前view所在的viewController

1
2
3
4
5
6
7
8
9
10
- (UIViewController*)viewController
{
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController*)nextResponder;
}
}
return nil;
}