11

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?

Nathan S.
  • 5,244
  • 3
  • 45
  • 55
Jasmeet
  • 1,522
  • 2
  • 22
  • 41
  • 1
    What code did you put in that method, what you've got there doesn't actually do anything and will actually probably not compile. – Fogmeister Apr 09 '14 at 16:47

6 Answers6

15

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];
rustylepord
  • 5,681
  • 6
  • 36
  • 49
  • 1
    no not working as desired. Let me explain, for example I am at tabitem_3 and I have to block tabitem_1 then This delegate doesnt stopt it for the First time, more over When I click this tabitem_1 again, the deelgate is not called – Jasmeet Apr 10 '14 at 05:40
  • Have a look at this as well,http://stackoverflow.com/questions/16786556/prevent-tabbar-from-changing-tab-at-specific-index-ios?rq=1 – rustylepord Apr 10 '14 at 06:14
  • works but not perfectly, i.e. when I click the Unlock tabs, it works fine, but when I click the the Locked tab, inspite of staying there it goes to the Previous Clicked Tab – Jasmeet Apr 10 '14 at 06:59
  • Note that `UITabBarControllerDelegate` needed – AykutE Oct 07 '15 at 18:25
9

Quick hack that i used to present a popup on top of the current view when tapped on a tab bar item.

In your TabBarViewController class, implement the UITabBarControllerDelegate and remember to set self.delegate = self.

After that

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
    if viewController.title == "Unique_title" //set an unique title for the view controller in storyboard or the view controller class.
    {
        performSegueWithIdentifier("YourModalViewIdentifier", sender: nil)
        return false
    } else {
        return true
    }
}

That should help you display a modal view when tap is received on the uitabbaritem. I know using title as an unique identifier is bad practice, but just a quick solution to achieve what you want.

Hope that helps.

mohonish
  • 1,396
  • 1
  • 9
  • 21
5

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];
  ...
}
mownier
  • 1,719
  • 2
  • 13
  • 27
  • Ok, it disables the Tab bar, But on the Click of that disabled TabBar Item, I have to show a pop up. How can I implement that? – Jasmeet Apr 10 '14 at 05:13
5

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

bicbmx
  • 817
  • 10
  • 8
5

This is what I did in Swift 2.1:

self.tabBarController!.tabBar.items![0].enabled = false
jaytrixz
  • 4,059
  • 7
  • 38
  • 57
0

Here is how you disable a tabbar item in Swift 3 and 4

tabBarController.tabBar.items![0].isEnabled = false
Zia
  • 14,622
  • 7
  • 40
  • 59