#######import <UIKit/UIKit.h>
@interface UIView (CurrentController)
/** 获取当前View的控制器对象 */
-(UIViewController *)getCurrentViewController;
@end
// 写在UIView分类中,所有继承自UIView的UI控件都可调用该方法获取其所在控制器
#######import "UIView+CurrentController.h"
@implementation UIView (CurrentController)
/** 获取当前View的控制器对象 */
-(UIViewController *)getCurrentViewController{
UIResponder *next = [self nextResponder];
do {
if ([next isKindOfClass:[UIViewController class]]) {
return (UIViewController *)next;
}
next = [next nextResponder];
} while (next != nil);
return nil;
}
@end