How to disable/enable a particular UITabBar Item,

Jasmeet picture Jasmeet · Apr 9, 2014 · Viewed 16.7k times · Source

I have 3 tabs in my UITabbarController, that I created in my Appdelegate.

When I open the app, I have made the selected tabbarItem the third tabbarItem.

The user can only select the UITabBarItem at Index 0, when he is logged in.

I tried every thing to restrict the user from going to TabBarItem_0 when he is at TabBarItem_2. But nothing worked. I used

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

}

But it's not working as I desired. I checked the stackoverflow and found almost the same question, where I found this delegate. But this is not working for me as desired. I googled, but couldn't find any solution other than stackoverflows links, which didn't help this time.

On the click of that disabled TabBar Item, I have to show a pop up. How can I implement that, too?

Answer

rustylepord picture rustylepord · Apr 10, 2014

Try something like this,

 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController; 
{    
       if (tabBarController.selectedIndex == 0) {
            if(isUserLoggedIn)
                return YES;
            else
                return NO;
        }

        return YES; 
}

If this does not work then,

Add this after you create the bar bar in app delegate,

[[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:FALSE];

once you log in enable it again

[[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:TRUE];