用一个分类,具体:
.h
#import <UIKit/UIKit.h>
@interface UIView (CurrentController)
/** 获取当前View的控制器对象 */
-(UIViewController *)getCurrentViewController;
@end
.m
#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