在iOS开发中经常会在自定义view中push到下一个导航控制器,这时就需要获取当前自定义view所在的导航控制器
- (UIViewController*)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UINavigationController class]]) {
return (UIViewController*)nextResponder;
}
}
return nil;
}
//获取导航控制器
- (UINavigationController*)navigationController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UINavigationController class]]) {
return (UINavigationController*)nextResponder;
}
}
return nil;
}