在ios开发中经常在自定义组件或者xib文件中切换到其他视图,那么就需要当前控制器,
- (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*)viewController {
for (UIView* next = [self superview];
next; next = next.superview) {
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UINavigationController class]])
{
return (UINavigationController*)nextResponder;
}
}
return nil;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-18 11:39:42