0

I am trying to figure out how to check which uitabbarbutton is currently selected before I let a user select another I would like them to deselect their previously chosen button.

This is the code that I am using to see which button gets selected..

#pragma TabBardelegatesht
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    switch (item.tag) {

        case 0:
        {
            NSLog(@"item 0 selected");

        }
            break;
        case 1:
        {
            NSLog(@"item 1 selected");
        }
            break;
        case 2:
        {

            NSLog(@"item 2 selected");
        }
            break;
        case 3:
        {
            NSLog(@"item 3 selected");
        }
            break;
        case 4:
        {
            NSLog(@"item 4 selected");
        }
            break;
        case 5:
        {
            NSLog(@"item 5 selected");

            // set up or remove jumpBar
            [self jumpBarButtonPosition:1];
        }
            break;
        default:
            break;
    }
}

so the question is, how do I stop another selection if a cell is selected and its not the one you are currently pressing?

any help would be hugely appreciated.

HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

1 Answers1

0

It seems that you should implement the -tabBarController: shouldSelectViewController: method of the Tab Bar's Delegate. For further reference check http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UITabBarControllerDelegate_Protocol/Reference/Reference.html

Remember you should set the delegate. If you need further help feel free to ask

EDIT:

The method I posted above does not meet the question requirements. If the user wants to be able to deselect all Tabs of the TabBar using [tabBar setSelectedItem:nil]; You can't use a Tab Bar View Controller.

EDIT 2: After trying out, i finally accomplished to code what the question was asking.

Interface:

@property (nonatomic) BOOL buttonsEnabled;
@property (nonatomic,weak) IBOutlet UITabBar *tabBar;

Implementation:

@synthesize tabBar = _tabBar;
@synthesize buttonsEnabled = _buttonsEnabled;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tabBar setDelegate:self];

    [self disableAllButtons];

    UITabBarItem *item = [self.tabBar.items objectAtIndex:0];

    [item setEnabled:YES];
    [self.tabBar setSelectedItem:item];
    self.buttonsEnabled = NO;

}

-(void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem *)item
{
    if(self.buttonsEnabled == NO)
    {
        [tabBar setSelectedItem:nil];
        [self enableAllButtons];
    }

    else {
        [self disableAllButtons];
        [item setEnabled:YES];
    }

}

-(void)enableAllButtons
{
    NSArray *items = [self.tabBar items];
    for (UITabBarItem* item in items)
        [item setEnabled:YES];

    [self setButtonsEnabled:YES];
}

-(void)disableAllButtons
{
    NSArray *items = [self.tabBar items];
    for (UITabBarItem* item in items)
        [item setEnabled:NO];

    [self setButtonsEnabled:NO];
}

The only thing is left to do is to set the UITabBar outlet in the storyboard/ib or if you alloc init it, just set the tabBar property to your tabBar. Remember not to use a UITabBarController. Hope that helps.

tomidelucca
  • 2,543
  • 1
  • 26
  • 26
  • I'm not sure how I could take use of **tabBarController:shouldSelectViewController:** for this problem do you have any tips. Thanks for pointing it out though.. I have been reading and think it might help with another thing I am working on .. but yea just not sure how it could be used here. – HurkNburkS Jul 11 '12 at 01:11
  • tabBarController:shouldSelectViewController gets called when the user presses a tab bar item. If you return NO, the selection won't take place. This way you can control if view controllers should be presented or not. I quite dont understand your last line "so the question is, how do I stop another selection if a cell is selected and its not the one you are currently pressing?". What are you actually trying to acomplish? – tomidelucca Jul 11 '12 at 01:21
  • sorry that dose sound confusing. basically if a tabbutton is selected, I dont want it to be possible for any of the other tabbutton to be selected until the initial button is deselected. Dose that make more sense? – HurkNburkS Jul 11 '12 at 01:33
  • Tab Bar buttons cant be deselected. There must always be one Tab Bar Button selected, and a View Controller shown. I think we are not talking about the same control. To deselect a Tab Bar button, you must select another one. – tomidelucca Jul 11 '12 at 01:41
  • No you can deselect a tabbar item using **[tabbarName setSelectedItem:nil];** to deselect the tabbar item.. I thought maybe I could use this somehow but I cannot figure out how to incorporate it into my code. – HurkNburkS Jul 11 '12 at 01:43
  • I have been trying to use **- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController** method.. but its never accessed even with the delegate set up in the header and then being assigned to the tabbar in .m viewdidload method. – HurkNburkS Jul 11 '12 at 02:26
  • i made further research and unfortunately you CAN'T implement [tabbarName setSelectedItem:nil]; if the tab bar is managed by a tab bar controller. It will work only if the tab bar is on its own. So sorry, the answer i provided you has no use. – tomidelucca Jul 11 '12 at 02:30