11

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

1
  • Have you tried popping to root view controller before you push? [[self navigationController] popToRootViewControllerAnimated:NO]; Commented Aug 4, 2015 at 14:53

2 Answers 2

3
+25

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you elaborate your statement For this goal you will need create new property for keeping currentNavigationController. I don't get what needs to be done...
I was talking about creating property in class: @property (nonatomic, strong) UINavigationController *currentNavigationController;
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.