1

I'm trying to deny a certain tabBarItem from pulling its action if there is no internet connection.

This is my code:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    ONLog(@"tab selected: %@", item.title);

    if (item.tag == 2) {
        if (![[InternetManager sharedManager] isInternetWorking]) {
            [self setSelectedIndex:1];
            UIAlertView *noInternet = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [noInternet show];
        }
    }
}

Issue is: The alert view is being called but the "setSelectedIndex" is not doing its work...

Any ideas?

2
  • do you get inside the second condition?
    – Julian
    Oct 23, 2014 at 8:13
  • of course... I said the alert view is being called :) Oct 23, 2014 at 8:14

1 Answer 1

0

Check what the value of self.selectedItem is when the method is first called. Maybe the selected item isn't actually set as selected until after this method is called, so [self setSelectedIndex:1] is being called but the item that the user tapped gets selected immediately after.

2
  • I'm trying to check apple docs to see if anything will help you. Am I correct in assuming that this is code for a UITabBar xib? I'm looking at something like tabBarController:shouldSelectViewController:, where you can simply return NO to stop the selection from occurring. Oct 24, 2014 at 17:58
  • I would recommend a better solution and will continue looking for one, but a quick fix could be adding a button on top of the particular UITabBarItem. Then inside of the btnPressed function you can call [self setSelectedIndex:2] to do the selection programmatically if [[InternetManager sharedManager] isInternetWorking] is true. Oct 24, 2014 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.