iOS 从appDelegate跳转其他子控制器

  1. 获取当前视图控制器
  • (UIViewController *)currentViewController {
    UIViewController *result = nil;
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    //app默认windowLevel是UIWindowLevelNormal,如果不是,找到UIWindowLevelNormal的
    if (window.windowLevel != UIWindowLevelNormal) {
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for(UIWindow *tmpWin in windows) {
    if (tmpWin.windowLevel == UIWindowLevelNormal) {
    window = tmpWin;
    break;
    }
    }
    }
    id nextResponder = nil;
    UIViewController *appRootVC=window.rootViewController;
    if (appRootVC.presentedViewController) {
    nextResponder = appRootVC.presentedViewController;
    } else {
    UIView *frontView = [[window subviews] objectAtIndex:0];
    nextResponder = [frontView nextResponder];
    }

    if ([nextResponder isKindOfClass:[UITabBarController class]]){
    UITabBarController *tabbar = (UITabBarController *)nextResponder;
    UINavigationController *nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
    result = nav.childViewControllers.lastObject;

    } else if ([nextResponder isKindOfClass:[UINavigationController class]]){
    UIViewController *nav = (UIViewController *)nextResponder;
    result = nav.childViewControllers.lastObject;
    } else {
    result = nextResponder;
    }
    return result;
    }

2.实现跳转到指定视图控制器
UIViewController * currentVC = [self currentViewController];
HYTAdditionalInsuranceViewController *addInsureVC = [[HYTAdditionalInsuranceViewController alloc] init];
[currentVC.navigationController pushViewController:addInsureVC animated:YES];

或者跳转到指定tabbarController
TabBarController *tabbar = [[TabBarController alloc] init];
tabbar.selectedIndex = 0;
self.window.rootViewController = tabbar;

你可能感兴趣的:(iOS 从appDelegate跳转其他子控制器)