开发者

How to stop tab bar's second tap which pops to navigation controller? [duplicate]

This question already has answers here: Prevent automatic popToRootViewController on double-tap of UITabBarController (5 answers) Closed 3 years ago.

I have a tab bar based app. All tabs have a navigation controller as the root. If the user taps on the tab again if the tab is active, it pops back to the navigation controller.

How can I stop this behavior?

So in fact I have a navigation controller + a hidden viewcontroller that makes some decisions + another view controller. Sorry for the misleading information in the original question. I use the hidden viewcontroller for all the tabs, 3 of them, since I have the login screen on all 3 if the user is not logged in. If the user logs in, then I pop the login screen, and put the 1,2,3 individual viewcontrollers on each tab.

First tap:

开发者_开发问答
 0 : class=Crossing: 0x645c8a0>  
 1 : class=FavoritesViewController: 0x64ac140>  
 shouldSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  
 myTabBarController.selectedViewController :UINavigationController  
 did disappear  
 didSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  

Second tap:

 0 : class=Crossing: 0x645c8a0>  
 1 : class=FavoritesViewController: 0x64ac140>  
 shouldSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  
 myTabBarController.selectedViewController :UINavigationController  
 didSelectViewController : UINavigationController  
 UINavigationController topclass:Crossing  


@MarkGranoff was on the right track for doing this, but the way to do it is by doing something like this:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([tabBarController.viewControllers indexOfObject:viewController] == tabBarController.selectedIndex)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

or in a less verbose way:

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

If you only want to block the default behaviour for a certain tab then you can do something like this:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSUInteger indexOfNewViewController = [tabBarController.viewControllers indexOfObject:viewController];
    // Only the second tab shouldn't pop home 
    return ((indexOfNewViewController != 1) ||
            (indexOfNewViewController != tabBarController.selectedIndex));
}


Swift 3

  1. Subclass your UITabBarController and implement UITabBarControllerDelegate on that class.

    class viewMain: UITabBarController, UITabBarControllerDelegate {

  2. In viewDidLoad, set your class's delegate to be itself

    self.delegate = self

  3. Add this function

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { return (viewController != tabBarController.selectedViewController); }


In your application delegate (which I assume is the delegate for your UITabBarController), try implementing tabBarController:shouldSelectViewController: and return NO if the view controller to be selected is not at its root view. This, of course, may backfire for tabs that were switched away from when you try to go back to them... Hmm....


You can check for the view controller in

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

and you can keep track on the view controllers selected.


Update Swift 4.1

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

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

Thanks!!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜