Prevent automatic popToRootViewController on doubl

2019-01-09 00:29发布

The default behavior of a UITabBarController is to pop the contained UINavigationController to the root view controller when a particular tab is tapped a second time. I have a particular use case where I'm wanting this to not work automatically, and I'm having a hard time figuring out how to prevent this.

Has anyone run into this, and if so, what did you do? Do I need to subclass UINavigationController and override popToRootViewController or is there a simpler way?

5条回答
混吃等死
2楼-- · 2019-01-09 00:59

this is what I did:

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

    if ([[tabBarController viewControllers] objectAtIndex:[tabBarController selectedIndex]] == viewController)

            return NO;

    return YES;

}

regards

查看更多
祖国的老花朵
3楼-- · 2019-01-09 01:04

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;
}
查看更多
Root(大扎)
4楼-- · 2019-01-09 01:14

Use the tabBarController:shouldSelectViewController: method of the UITabBarControllerDelegate protocol.

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

Don't forget to set the delegate of the tab bar controller to the object that actually implements this delegate method.

查看更多
【Aperson】
5楼-- · 2019-01-09 01:15

Update Swift 4.1

Stop Double Tap for all tabs.

extension TabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    //for blocking double tap on all tabs.
    return viewController != tabBarController.selectedViewController
}}

Stop Double Tap on only one specific tab. Here it's for 3rd Tab.

extension TabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    //for blocking double tap on 3rd tab only
    let indexOfNewVC = tabBarController.viewControllers?.index(of: viewController)
    return ((indexOfNewVC != 2) ||
        (indexOfNewVC != tabBarController.selectedIndex))       
}}

Hope it helps...

Thanks!!!

查看更多
狗以群分
6楼-- · 2019-01-09 01:19

Here is the Swift 3 version:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    return viewController != tabBarController.selectedViewController
}
查看更多
登录 后发表回答