美文网首页
使用tabBarController.selectedIndex

使用tabBarController.selectedIndex

作者: 咱也不知道 | 来源:发表于2019-08-01 10:24 被阅读0次

    今天发现使用了self.navigationController.tabBarController.selectedIndex = 0;

    出现一个问题 如果有四个tabbaritem 首页,  活动, 朋友圈 , 我的   当我在我的tabbar 里面的二级控制调用self.navigationController.tabBarController.selectedIndex = 0; 这个方法回到首页 然后再点击我的会出现 还在二级界面 没有回到我的页面 由于需求需要回到我的页面  经过苦心思索累死累活 一开始用了一个笨方法 直接每次如果有这样奇葩跳转时候进行重新初始化tabbarController 虽说解决问题了 但是这绝对不是正确的解决办法 于是乎又产生另一种方法 就是在调用完tabBarController.selectedIndex = 0; 这个方法后 获取tabBarController的viewControllers  然后遍历这个 viewControllers 判断是否有UINavigationController  如果存在 那么调用 popToRootViewController   贴上代码:

    [self currentViewController].navigationController.tabBarController.selectedIndex = 0;

                NSArray *AllControllers = [self currentViewController].navigationController.tabBarController.viewControllers;

                for(UIViewController *viewController in AllControllers)

                {

                    if([viewController isKindOfClass:[UINavigationController class]])

                    {

                        UINavigationController *nav= (UINavigationController *)viewController;

                        [nav popToRootViewControllerAnimated:YES];

                    }

                }

    虽然很简单但是 我却想的要死要活的惭愧呀!!!

    相关文章

      网友评论

          本文标题:使用tabBarController.selectedIndex

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