Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UITabbar item double-click pops controllers

just found out something: If you have a Tabbar combined with a NavigationController (that has some views on it's stack) and you double click the TabBarItem, the view pops to the first ViewController, whether you like it or not.

Is there a way to prevent this?

like image 384
Chris Avatar asked Jun 23 '09 19:06

Chris


2 Answers

You probably should not prevent this behavior. It's a standard iPhone UI convention, like tapping the status bar to scroll to the top of a scroll view.

If you really want to do it, you should implement the UITabBarController delegate method -tabBarController:shouldSelectViewController:, like mckeed mentioned. If you have more than five tabs, however, the selectedViewController may be a view controller that is in the "More" section, but vc will be [UITabBarController moreNavigationController]. Here's an implementation that handles that case:

- (BOOL)tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)vc {
    UIViewController *selected = [tbc selectedViewController];
    if ([selected isEqual:vc]) {
        return NO;
    }

    if ([vc isEqual:[tbc moreNavigationController]] &&
        [[tbc viewControllers] indexOfObject:selected] > 3) {
        return NO;
    }

    return YES;
}
like image 136
lemnar Avatar answered Oct 04 '22 20:10

lemnar


I just ran into this problem myself and found a way to do it. Make a delegate for your UITabBarController and implement tabBarController:shouldSelectViewController: to prevent selecting the same controller:

- (BOOL) tabBarController:(UITabBarController *)tbc shouldSelectViewController:(UIViewController *)vc {
  return tbc.selectedViewController != vc;
}

You can also add more complicated logic if you only want to prevent it in some cases.

like image 27
mckeed Avatar answered Oct 04 '22 22:10

mckeed