iOS 获取View所在控制器UIViewController

  • 方法
    -(UIViewController *)viewController
    {
    for (UIView *next = [self superview]; next; next = next.superview) {
    UIResponder *nextResponser = [next nextResponder];
    if ([nextResponser isKindOfClass:[UIViewController class]]) {
    return (UIViewController *)nextResponser;
    }
    }
    return nil;
    }

  • 调用
    SecondViewController *Second = [[SecondViewController alloc] init];
    UIViewController *controller = [self viewController];//调用
    [controller.navigationController pushViewController: Second animated:YES];

你可能感兴趣的:(iOS 获取View所在控制器UIViewController)