Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable action - user taps on tabbar item to go to root view controller

I want to disable the default action when user taps the tabbar item.

For example, i have a tabbar with Tab1, Tab2 and Tab3. In Tab1, user can navigate from View1 to View3 (View1 > View2 > View3). If user is at View3, and he taps the Tab1, the application takes the user to View1 (the root view controller). I want to disable this functionality. I don't want the tap on Tab1 to pop all the view controllers. How can i do that?

Edit:

This behavior is a little strange, but a handy shortcut in case of deep hierarchy!

You can implement following UITabBarControllerDelegate methods to disable this system wide shortcut:

#pragma mark -
#pragma mark UITabBarControllerDelegate

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

    if ([tbSelectedController isEqual:vc]) {
        return NO;
    }

    return YES;
}
like image 704
Mustafa Avatar asked Nov 16 '10 05:11

Mustafa


People also ask

How do I add a tab bar to my view controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

What is Tabbar controller?

A Tab Bar Controller is a container view controller that manages an array of view controllers in a radio-style selection interface so that the user can interact with the interface to determine the view controller to display. It is the instance of UITabBarController, which inherits UIViewController.

How do I hide a Tabbar in Swift?

Simply, Go to ViewController (in StoryBoard) -> Attribute inspector -> Under 'View Controller' section select 'Hide Bottom Bar on Push' checkbox.

How do I hide the bottom Tabbar in Swift?

If you don't want that behavior, you should set hidesBottomBarWhenPushed to true where applicable. This will hide the tab bar along with any toolbars you had showing, but only when a view controller is pushed onto the navigation stack. This allows you to show the tab bar at first, then hide it when you need more room.


1 Answers

if you look at the UITabBarController delegate there is a method:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

If you implement this in your class, you can check if the UIViewController is the already displayed one and then return NO, which will stop this from happening.

I had the same problem with a ABPeoplePicker object embedded in a UITabBarController, in that pressing the 'Contacts' tab a second time which was already displayed would make the ABPeoplePicker control show the 'Groups'

like image 149
Tony Million Avatar answered Sep 25 '22 17:09

Tony Million