Multiple Buttons, But only one pressed at a time

1.5k Views Asked by At

I've been looking through the site and I've been unable to find anything that answers my question. In my app i have a modal dialog that displays a UICollectionView that uses a custom cell and only contains a UIButton.

I have set the buttons up to be selectable rather than single click. I want to only allow a single button to be selected at a time and when a button is selected I want the other buttons in that section to be disabled. I'm not sure how to go about this as I am still fairly new to IOS. The screen shot below shows the basic appearance of the model and the buttons.

At the moment I can select multiple buttons but I need it to be single selection and when one button is selected the rest are disabled. For the moment I hand the selected in the class for my custom UICollectionViewCell and I have included it below. Any help and suggestions would be great i just need pointing in the right direction.

- (IBAction)Selected:(id)sender {
    if(_Button.selected == 0)
    {
        [_Button setSelected:YES];        
    }
    else
    {
        [_Button setSelected:NO];
    }
}

enter image description here

3

There are 3 best solutions below

0
On BEST ANSWER

In collection view datasource method - collectionView:cellForItemAtIndexPath: when you are configuring the cell, set the indexPath item value as tag of the corresponding button in the cell.

Then maintain a property say, previousSelectedButtonIndex in which you can maintain the value of the previously selected index.

In your button click handler, if previousSelectedButtonIndex contains a value, and if yes, deselect that button, select new button and set previousSelectedButtonIndex to the new value.

Please note, you might have to do some error handling, but basically this is how it can be implemented.

0
On

Declare one global variable as

NSInteger selectedIndex;

also set -1 value to selectedIndex as initially no button is selected.

selectedIndex = -1;

Then write in datasource method of UICollectionView

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
 {
    //here create cell object of UICollectionViewCell
    cell.button.tag = indexPath.row;
    [cell.button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    if(indexPath.row == selectedIndex)
    {
      //show this button as selected by setting different name/color/image for button
       cell.button.enabled = TRUE;
    }
    else
       cell.button.enabled = FALSE;
    return cell;
}


- (void) buttonPressed:(id)sender
  {
    UIButton *selectedButton = (UIButton *)sender;
    if(selectedIndex == selectedButton.tag)
        selectedIndex = -1; //deselect the selected button
    else
        selectedIndex = selectedButton.tag; //select the tapped button
    [self.collectionView reloadData];
  }
0
On

You should probably use a collection of buttons. Apple have given the ability to control a series of buttons at the same time. Try reading this to get an idea on how you can achieve this. Outlet Collections