Disable action - user taps on tabbar item to go to

2019-01-24 00:55发布

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;
}

1条回答
Viruses.
2楼-- · 2019-01-24 01:46

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'

查看更多
登录 后发表回答