iOS-获取子视图父控制器

开发中有的时候需要涉及当前视图的父级视图,可以通过UIResponder来获取,有两种实现方式:

UIView *next=sender;
while ([next superview]) {
    next=[next superview];
    UIResponder *nextResponder = [next nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]&&![nextResponder isKindOfClass:[UINavigationController class]]) {
        Class cls=[nextResponder class];
        NSLog(@"ClassName:%@",cls);
    }
    NSLog(@"FlyElephant---%@",next);
    NSLog(@"FlyElephant---%@",nextResponder);
}

还有一种类似的实现方式如下:

for (UIView *next = [sender superview]; next; next = next.superview) {
    NSLog(@"UIView---%@",next);
    UIResponder *nextResponder = [next nextResponder];
    NSLog(@"UIResponder---%@",nextResponder);
}

你可能感兴趣的:(iOS-获取子视图父控制器)