首页 > 解决方案 > ObjC如何在选择TabBarItem时弹回RootViewController

问题描述

我得到了以下流程

在家选项卡选择晚上叫[self.parentViewController.tabBarController setSelectedIndex:2];去上课选项卡

在家里选择晚上

在类选项卡中选择任何类

在班级选择一个班级

去下一个VC

下一个风投

再次选择家,再次选择晚上

选择家并再次选择晚上

留在之前的VC没有回到RootView 留在下一个VC

应该在这里 应该是 RootViewController

Home Tab我执行[self.parentViewController.tabBarController setSelectedIndex:2];Class Tab。然后从嵌入的 Class Tab 中,NavigationController我将使用seague它转到下一个 VC 及以后。

但是当我Home Tab再次选择时,我想Class Tab回到RootViewController

我尝试了以下方法,但它不起作用。RootViewController每次下一次VC消失时,它都会一直弹出。

   -(void) viewWillDisappear:(BOOL)animated {

       [self.navigationController popViewControllerAnimated:YES];

       [super viewWillDisappear:animated];
   }

我有以下代码 MyTabBarController ,这是由一位堆栈溢出大师给我的,但不确定每次TabBarController选择新时在哪里调整以返回 RootViewController Class.m 选项卡。请帮忙。

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"didSelectViewController... ");

//==== Tried this but not triggering =====    
//[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
//if ([viewController isKindOfClass:[UINavigationController class]]) {
    //[self.navigationController popViewControllerAnimated:YES];
//}
//==========================================

    NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
    NSLog(@"controller title: %@", viewController.title);

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

        // we're expecting a nav controller so cast it to a nav here
        UINavigationController *navController = (UINavigationController *)viewController;

        // now grab the first view controller from that nav controller
        UIViewController *firstViewControllerInNav = navController.viewControllers.firstObject;

        // check to make sure it's what we're expecting (ParentViewController)

       if ([firstViewControllerInNav isKindOfClass:[ParentViewController class]]) {
            // cast it to our parent view controller class
            [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
            ParentViewController *viewControllerToCallMethodOnAfterSelection = (ParentViewController *)firstViewControllerInNav;
            [viewControllerToCallMethodOnAfterSelection doStuffWhenTabBarControllerSelects];
        }else{
        //=== The following code will make viewWillAppear load on each tab bar item
        //=== Without it, tapping on new tab bar item will not load viewWillAppear
            [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
        }
    }


}

添加了以下代码,它会触发但不会将 selectedIndex = 2 带回 Root

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    NSUInteger shouldSelectIndex = [tabBarController.viewControllers indexOfObject:viewController];
    //Check if current index is Class tab and new index is Home
    if (tabBarController.selectedIndex == 2 && shouldSelectIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
        //[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
        //[tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex];
    }
    return YES;
}

添加了故事板

故事板

添加的文件结构

文件结构

标签: iosobjective-cuinavigationcontrolleruitabbarcontroller

解决方案


在您的 tabbarController 中创建一个方法,并在您希望类选项卡访问其根视图控制器时调用此方法

-(void)popToClassRootViewController{
    // Considering the fact that class view contorller will always be on 3 no and will be of UINavigationController
    UINavigationController *classNavController = (UINavigationController *)[self.viewControllers objectAtIndex:2];
    // You can get the navigation controller reference by any way you prefer
    [classNavController popToRootViewControllerAnimated:false];
}

在您的情况下,我认为您想在单击其他选项卡时重置视图控制器,以便您可以使用选项卡委托方法来检查是否单击了其他选项卡栏项目并调用该方法

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
    if (index != 2) {
       //Note: Call this method according to your need in this case it will be called whenever user will select tab other then Class
       [self popToClassRootViewController];
    }
    return true;
}

推荐阅读