ios - ObjC如何在选择TabBarItem时弹回RootViewController
问题描述
我得到了以下流程
在家选项卡选择晚上叫[self.parentViewController.tabBarController setSelectedIndex:2];
去上课选项卡
在类选项卡中选择任何类
去下一个VC
再次选择家,再次选择晚上
从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;
}
添加了故事板
添加的文件结构
解决方案
在您的 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;
}