IOS 常用的界面之间的跳转总结

2018-02-07  本文已影响68人  MrLiangC

一、模态推送

模态视图之间的切换利用– presentViewController:animated:completion:弹出视图,– dismissViewControllerAnimated:completion返回,这是系统提供的最基本的推送方式,但是开发中常常会遇到利用模态视图进行多个页面跳转后,要返回到最初的那个界面或者发回到指定的界面,这需要了解视图控制器的两个属性presentedViewControllerpresentingViewController比如A—>B 那么A相对于B就是presentingViewController,B相对于A是presentedViewController

比如:A->b->c->d, 然后d要返回到A或者返回到指定界面,像这种多次present后,dismiss到指定界面做法如下

- (void)disMissToViewController:(UIViewController *)viewController {
    UIViewController *vc = self.presentingViewController;
    NSLog(@"%@",[vc class]);
    
    while (![vc isKindOfClass:[viewController class]]) {
        NSLog(@"%@",[vc class]);
        vc = vc.presentingViewController;
    }
    
    [vc dismissViewControllerAnimated:YES completion:nil];
}
-(void)dismissToRootViewController
{
    UIViewController *vc = self;
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:nil];
}

二、导航控制器

- (UIViewController *)presentingVC{
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIViewController *result = window.rootViewController;
    while (result.presentedViewController) {
        result = result.presentedViewController;
    }
    if ([result isKindOfClass:[UITabBarController class]]) {
        result = [(UITabBarController *)result selectedViewController];
    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        result = [(UINavigationController *)result topViewController];
    }
    NSLog(@"%@",[result class]);
    return result;
}
- (void)goToVC:(UIViewController *)viewController {
    if (!viewController) {
        return;
    }
    //获取当前根视图控制器
    UINavigationController *nav = [self presentingVC].navigationController;
    if (nav) {
        [nav pushViewController:viewController animated:YES];
    }
}
- (void)popToViewController:(UIViewController *)viewController {
    
    
    for (UIViewController * controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[viewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}

如果在View层想要获取view所在的当前控制器

- (UIViewController *)viewController {
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        
        UIResponder *nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            
            return (UIViewController *)nextResponder;
            
        }
    }
    return nil;
}
- (UIViewController *)currentViewController {
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    // modal展现方式的底层视图不同
    // 取到第一层时,取到的是UITransitionView,通过这个view拿不到控制器
    UIView *firstView = [keyWindow.subviews firstObject];
    UIView *secondView = [firstView.subviews firstObject];
    UIViewController *vc = [secondView viewController];
    
    if ([vc isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tab = (UITabBarController *)vc;
        if ([tab.selectedViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
            return [nav.viewControllers lastObject];
        } else {
            return tab.selectedViewController;
        }
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)vc;
        return [nav.viewControllers lastObject];
    } else {
        return vc;
    }
    return nil;
}
上一篇下一篇

猜你喜欢

热点阅读