【问题标题】:ObjC how to pop back to RootViewController when TabBarItem is selectedObjC如何在选择TabBarItem时弹回RootViewController
【发布时间】:2019-07-08 00:30:51
【问题描述】:

我得到了以下流程

在主页选项卡选择晚上名为 [self.parentViewController.tabBarController setSelectedIndex:2]; 以转到课堂选项卡

在类选项卡中选择任何类

转到下一个 VC

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

留在之前的 VC 没有回到 RootView

应该在这里

Home Tab 我执行[self.parentViewController.tabBarController setSelectedIndex:2];Class Tab。然后从嵌入在NavigationController 中的类选项卡中,然后我将使用seague 转到下一个VC 并继续。

但是当我再次选择Home Tab 时,我希望Class Tab 回到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 带回 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;
}

添加了故事板

添加的文件结构

【问题讨论】:

  • 你试过viewController.navigationController吗?不确定您是如何安排故事板的,但如果每个选项卡都有自己的 navigationController 作为根目录,它应该可以工作
  • 是的,每个选项卡都有自己的导航控制器,将 viewController.navigationController 放在哪里?
  • 啊,我以为你的代码不工作,它工作但每次你点击新标签时它都会返回是吗?因此,您真正需要的是,您希望每次用户选项卡都不是Class 选项卡,那么Class 选项卡应该回到根目录?
  • Home Tab,我将单击buttonEvening,它将执行[self.parentViewController.tabBarController setSelectedIndex:2] 并带我到Class Tab 当我单击ClassVC 上的链接时,它将执行segue到另一个VC1。当我单击主页选项卡时。我想让VC1 回到ClassVC or RootView。如果不是,当我在HomeTab 而不是ClassVC or RootView 再次单击buttonEvening 时,我将转到VC1。我的想法是选择HomeTab VC1返回ClassVC。 span>

标签: ios objective-c uinavigationcontroller uitabbarcontroller


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果我理解正确,那么如果当前选项卡是 ClassHome 选项卡被选中,您想返回根视图。也许这会比使用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;
    }
    

    【讨论】:

    • 抱歉,我需要添加代理吗?并调用类似'self.delegate = self'的东西?
    • 这是 tabbarController 的委托函数,所以如果你的意思是 self = tabbarController 或者用这个替换 didSelectViewController 是的
    【解决方案3】:

    使用下面的 swift 代码清除导航栏控制器数组

    guard let child = self.childNavController else {return}

    child.viewControllers = []

    您可以在 tabbar didselect 方法的目标 C 中使用相同的用户

    【讨论】:

    • 对不起,我是个菜鸟。请您详细说明一下吗
    • 如何让索引 2 处的 tabBarController 进入根目录。在索引 0,我已经这样做了。基本上,从索引2回到索引0,如何让index2弹出到root,然后再切换到索引0。
    • NSMutableArray *tbViewControllers = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]]; [tbViewControllers removeObjectAtIndex:2]; [self.tabBarController setViewControllers:tbViewControllers];
    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多