I am making an iOS app where I have Tab bar + Side Menu.
Tab bar have 5 items and side menu have around 12 menus.
All side menu functionalities are from Tab 1 & side menu is accessible across all views in tab bar.
That means if I am on Tab 2, even I can access side menu. When I click on side menu item from Tab 1, I will go to Tab 1 and then navigation will occur.
What I want to do is let's say if I click on Complains menu from side menu, I want to go to ComplainsViewController.
Code I used is as below.
// go to first tab
self.tabBarController.selectedIndex = 0;
// now navigate
ComplainsViewController *sViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Complains"];
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:sViewCon animated:NO];
I have two scenario.
Scenario 1 (Correct)
I am on Tab 1 and click on Complains from side menu. When I click, I go successfully to ComplainsViewController using above code.
Scenario 2 (In-Correct)
I am on Tab 2 and click on Complains from side menu. When I click, I go successfully to Tab 1, but I don't navigate to ComplainsViewController. When I click back to Tab 2, I see ComplainsViewController open in Tab 2.
Any idea how to switch first to Tab and then navigate to another viewcontroller?
Below is the basic structure I have.
Looks like you forgot to switch navigationController
after switching tabs.
Try to implement UITabBarControllerDelegate
method:
- (void)tabBarController:(UITabBarController * _Nonnull)tabBarController
didSelectViewController:(UIViewController * _Nonnull)viewController {
self.currentNavigationController = (UINavigationController *)viewController;
}
Then when you manually switch tabs use this code:
// go to first tab
self.tabBarController.selectedIndex = 0;
// Manually call delegate method
[self tabBarController:self.tabBarController didSelectViewController:self.tabBarController.selectedViewController];
// now navigate
ComplainsViewController *sViewCon = [self.storyboard instantiateViewControllerWithIdentifier:@"Complains"];
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionFade;
[self.currentNavigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.currentNavigationController pushViewController:sViewCon animated:NO];
For this goal you will need create new property for keeping currentNavigationController
.
Finally I managed to get it solved in another way with use of NSNotificationCenter
Below is what I did.
Step 1 (In SideMenuViewController)
// let first go to first index
self.tabBarController.selectedIndex = 0;
// now post notification and make transition
[[NSNotificationCenter defaultCenter] postNotificationName:@"TakeMeToComplains" object:nil];
Step 1, will change the selectedIndex and post the notification.
Step 2 (In all other view controllers)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(globalTakeMeToComplains) name:@"TakeMeToComplains" object:nil];
-(void)globalTakeMeToComplains {
// here I have code for Transition to go to ComplainsViewController
}
In Step 2, when we switch to first tab bar, viewWillAppear get called where I am listening to the notification. Once I hear a notification, I will execute the transition to another view controller.
Do let me know if anyone is not clear on what I am doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With