0

I have an UINavigationController inside of a TabBarController and when i select the already selected tabBarItem, the NavigationController pops back to its rootViewController. This is an automatic behavior as far as I know.

I need to modify this behavior, and using the

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

to push again the viewController i want, does not work properly since my push happens paralel with the automatic pop.

Hooloovoo
  • 86
  • 4

2 Answers2

0

I dont know how to change the behavior, but I strongly suggest you dont mess with such default behaviors. If Apple has not provided an easy way to mess with this behavior, then there is a reason for it.

Literally every voiceover user relies on gestures, and I'd assume one among them is double tap a tab to go to rootView. Messing with any of these default gestures, you are guaranteed to piss off a bunch of VO users. Unfortunately, I learnt this the hard way (using custom navbar with custom back button).

Hope this answer convinces you to reconsider your requirement =)

Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52
0

The solution is to subclass UINavigationController, and use your subclass with the UITabBarController. I threw in a couple other useful features.

And its just fine to do this - my app has 5 stars and no one has complained about it:

@implementation MyNavigationController

// This suppresses the normal pop to the root view controller
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
{   
    return @[];
}

// Extra: give my base classes some notice this is going to happen
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    UIViewController *vc = self.topViewController;
    if ([vc respondsToSelector:@selector(viewControllerWillBePopped)]) {
        [vc performSelector:@selector(viewControllerWillBePopped)];
    }

    return [super popViewControllerAnimated:animated];
}

// Extra: let the UIViewController refuse to pop
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    BOOL ret = YES;

    UIViewController *vc;
    for(UIViewController *obj in self.viewControllers) {
        if(obj.navigationItem == item) {
            vc = obj;
            break;
        }
    }

    if ([vc respondsToSelector:@selector(shouldPop)]) {
        NSNumber *retVal = [vc performSelector:@selector(shouldPop)];
        ret = [retVal boolValue];
        if(!ret) return NO;
    }
    return [super navigationBar:navigationBar shouldPopItem:item];
}

@end
David H
  • 40,852
  • 12
  • 92
  • 138