美文网首页
iOS 从appDelegate跳转其他子控制器

iOS 从appDelegate跳转其他子控制器

作者: 可乐小子 | 来源:发表于2019-08-21 09:53 被阅读0次
  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跳转其他子控制器

      本文链接:https://www.haomeiwen.com/subject/qmavsctx.html