Reputation: 811
I got the following flow
At Home Tab Select Evening called [self.parentViewController.tabBarController setSelectedIndex:2];
to Go to Class Tab
At Class Tab select any Class
Go to the next VC
Select Home Again and Evening Again
Remain at the previous VC did not go back to RootView
From Home Tab
i perform a [self.parentViewController.tabBarController setSelectedIndex:2];
to Class Tab
. Then from Class Tab which is embedded in NavigationController
I will then use seague
to go to next VC and onwards.
But when I select the Home Tab
again I want the Class Tab
to go back to RootViewController
I have tried the following but it is not working. It will just keep popping to the RootViewController
every time the next VC
Disappear.
-(void) viewWillDisappear:(BOOL)animated {
[self.navigationController popViewControllerAnimated:YES];
[super viewWillDisappear:animated];
}
I have the following code MyTabBarController which was given to me by a kind stack overflow guru but not sure where to tweak to Go back to RootViewController Class.m tab every time new TabBarController
is selected. Please help.
- (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];
}
}
}
Added the following code, it does fire but doesn't bring selectedIndex = 2 back to 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;
}
Added StoryBoard
Added File Structure
Upvotes: 0
Views: 720
Reputation: 3526
Create a method in your tabbarController and call this method when ever you want the class tab to get to its 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];
}
In Your case i think you want to to reset the view controller whenever other tab is clicked so you can use the tabbar delegate method to check if other tabbar bar items are clicked and call that method
- (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;
}
Upvotes: 1
Reputation: 36
Clear navigation bar controller array by using below swift code
guard let child = self.childNavController else {return}
child.viewControllers = []
you can user same in objective C in tabbar didselect method
Upvotes: 0
Reputation: 9923
If I understand correctly then you want to goes back to root view if current tab is Class
and Home
tab got selected. Maybe this would be better than using 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;
}
Upvotes: 0