ios - ObjC 如何在选择 TabBarItem 时弹出回 RootViewController

标签 ios objective-c uinavigationcontroller uitabbarcontroller

我得到了以下流程

在“主页”选项卡上选择名为“[self.parentViewController.tabBarController setSelectedIndex:2];”的“晚上”以转到“类(class)”选项卡

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选项卡时,我希望Class选项卡返回到RootViewController

我已尝试以下方法,但不起作用。每次下一个 VC 消失时,它都会继续弹出到 RootViewController

   -(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 返回到根目录

-(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

最佳答案

在 tabbarController 中创建一个方法,并在您希望类选项卡访问其 Root View Controller 时调用此方法

-(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];
}

在您的情况下,我认为您希望在单击其他选项卡时重置 View Controller ,以便您可以使用选项卡栏委托(delegate)方法来检查其他选项卡栏项目是否被单击并调用该方法

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

关于ios - ObjC 如何在选择 TabBarItem 时弹出回 RootViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54682636/

相关文章:

iOS更改导航栏标题字体和颜色

iphone - Objective-C 约定 : When to use For and when to use With

ios - iCarousel 居中项目的位置 (iOS)

ios - 以编程方式推送 UINavigationController 后顶部栏不可见

iphone - 核心数据 - 保存在后台线程并在主线程中获取

android - iOS 应用签名和所有权

ios - MPMoviePlayerController播放失败时如何获取错误描述

iphone - ios中的ASCII解码

ios - 以编程方式将 navigationController 添加到指定 viewController,无需 Storyboard swift

ios - 如何检测 UIWebView 中的点击事件