Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable/enable a particular UITabBar Item,

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?

like image 425
Jasmeet Avatar asked Apr 09 '14 16:04

Jasmeet


2 Answers

You can do this in your code

- (void)viewDidLoad {
    ...
    [self checkLogin];
    ...
}

- (void)checkLogin {
    if (!loggedIn) {
        [[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:NO];
    } else {
        [[[[self.tabBarController tabBar]items]objectAtIndex:0]setEnabled:YES];
    }
}

- (void)tapLogin {
  // Do the login action
}

- (void)processLoginResult {
  // Process the result of the login action
  // If the result is success, set 'loggedIn = YES'
  // Otherwise, set 'loggedIn = NO'
  ...
  [self checkLogin];
  ...
}
like image 129
mownier Avatar answered Sep 25 '22 13:09

mownier


If you want to do it with the Storyboard, simply selected the TabBarItem in the destination view controller scene and uncheck the Enabled box.

like image 23
bicbmx Avatar answered Sep 23 '22 13:09

bicbmx