pdrcabrod
pdrcabrod

Reputation: 1477

Tab bar app. Is it possible to push/pop view controllers in different navigation controllers?

Im trying to achieve the following:

Lets imagine we are in a navigation controller and we have a ViewController pushed. The user taps in another items of the tab bar. BEFORE we move to another view controller, I want to pop (animated) the pushed view controller, BUT I want the new view controller as a root so it´s appear as the pushed one goes away.

Its hard to explain, I just want the new view controller to appear when the pushed one is disappearing due to the animation of popToRootViewController instead of the old one appearing and then just moving to the new one.

Imagine we are in the option b in the tab bar which corresponds to the view controller B, and has a C view controller pushed.

A

B -> C

The users presses a,

I want this to happen

A -> C

B -> C

Then select A viewController, and then pop both C´s, animated in the A case, and not animated in the B case.

This is my code:

    [(UINavigationController *)self.tabBar.selectedViewController popToRootViewControllerAnimated:NO];
     UIViewController * viewControllerToPush = self.tabBar.selectedViewController;

    self.tabBar.selectedIndex = indexPath.row;

    [(UINavigationController *)self.tabBar.selectedViewController pushViewController:viewControllerToPush animated:NO];
    [(UINavigationController *)self.tabBar.selectedViewController popToRootViewControllerAnimated:YES];

But its not working, I get 'Pushing a navigation controller is not supported'

Is it possible to move viewControllers from one navigation controller to another? Any ideas?

Upvotes: 0

Views: 1275

Answers (2)

ader
ader

Reputation: 5393

I think that's very bad UI design and Apple would disapprove and that you would be violating the Apple Human Interface Guidelines (HIG). I think that your app would behave different to how your users would expect.

TabBarControllers individual tabs are for discrete/different areas of an app, not for moving around a single area of an app. Hopefully that makes sense.

I would suggest you'd be better off using a single navigationController with a toolBar at the bottom in place of the tabBar then just push pop within the one navigation stack.

Upvotes: 0

Yaman
Yaman

Reputation: 3991

Take a look at UINavigationControllerDelegate and UITabBarControllerDelegate methods. They will allow you to perform some actions when pushing/popping a controller or when selected a tab :

UINavigationControllerDelegate

– navigationController:willShowViewController:animated:
– navigationController:didShowViewController:animated:

UITabBarControllerDelegate

– tabBarController:shouldSelectViewController:
– tabBarController:didSelectViewController:
– tabBarController:willBeginCustomizingViewControllers:
– tabBarController:willEndCustomizingViewControllers:changed:
– tabBarController:didEndCustomizingViewControllers:changed:

Upvotes: 0

Related Questions