美文网首页
iOS push和present同时存在,返回根视图控制器

iOS push和present同时存在,返回根视图控制器

作者: 灯红酒绿映不出的落寞 | 来源:发表于2019-03-06 15:58 被阅读5次

    需求如下:

    1.firstVC使用push跳转到secondVC
    2.secondVC使用present弹起thirdVC
    3.thirdVC使用present弹起fourVC
    4.fourVC界面点击按钮返回firstVC(根视图)

    效果图
    Untitled4.gif
    核心代码
    最后一个present的vc需要返回主视图时调用
    -(void)toRootViewController{
        UITabBarController *tabBarController = (UITabBarController *)[UIApplication sharedApplication].delegate.window.rootViewController;
        if ([self respondsToSelector:@selector(presentingViewController)]){
            self.presentingViewController.view.alpha = 0;
            [self.presentingViewController.presentingViewController dismissViewControllerAnimated:false completion:^{
                
                for (UIViewController * vc in tabBarController.selectedViewController.childViewControllers) {
                    if ([vc isKindOfClass:[ViewController class]]) { // 第一个controller
                        [tabBarController.selectedViewController popToViewController:vc animated:false];
                    }
                }
            }];
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS push和present同时存在,返回根视图控制器

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