Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch tab bar before navigation to another viewcontroller

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?


Edit 1

Below is the basic structure I have.

enter image description here

like image 548
Fahim Parkar Avatar asked Aug 02 '15 11:08

Fahim Parkar


2 Answers

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.

like image 136
Vlad Papko Avatar answered Nov 15 '22 13:11

Vlad Papko


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.

like image 27
Fahim Parkar Avatar answered Nov 15 '22 14:11

Fahim Parkar