ObjC当选择TabBarItem时如何弹出回到RootViewController

时间:2019-02-14 03:07:53

标签: ios objective-c uinavigationcontroller uitabbarcontroller

我得到以下流程

在“首页”标签上,选择“ [self.parentViewController.tabBarController setSelectedIndex:2];之夜”以转到“课程标签”

At Home Select Evening

在“班级”标签上,选择任何班级

At Class select a class

转到下一个VC

Next VC

再次选择住所并再次选择晚上

Select Home and select Evening Again

在以前的VC中保留的内容未返回RootView Remain At the next VC

应该在这里 ShouldBe RootViewController

我从Home Tab[self.parentViewController.tabBarController setSelectedIndex:2];执行Class Tab。然后,从NavigationController中嵌入的Class Tab中,我将使用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;
}

添加了StoryBoard

Story Board

添加的文件结构

File Structure

3 个答案:

答案 0 :(得分:0)

如果我的理解正确,那么如果当前标签为Class并且选择了Home标签,那么您想返回到根视图。也许比使用didSelectViewController更好:

- (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) {
       //Pop to root code with `[tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex]` (Class tab's navigation controller)
    }
    return YES;
}

答案 1 :(得分:0)

使用以下快速代码清除导航栏控制器数组

警卫队让child = self.childNavController else {return}

child.viewControllers = []

您可以在选项卡的didselect方法中的目标C中使用相同的

答案 2 :(得分:0)

在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;
}