用一個分類,具體:
.h
#import
@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