Search code examples
iosuibuttonxcode6ibaction

iOS: How to handle multiple buttons created dynamically with one iBAction?


I have created radio and check box buttons programmatically according to my web service response, where number of buttons varies.

Following is the code to create those buttons:

for(int j = 0; j < nintOptionCount; j++)
    {
        UILabel * lblOption =  [[UILabel alloc] initWithFrame: CGRectMake(50, yLabel, 250, 21)];
        //lblOption.backgroundColor = [UIColor yellowColor];
        lblOption.text = [arrmOptionName objectAtIndex:j];
        lblOption.textColor = [UIColor blackColor];
        lblOption.font = [UIFont systemFontOfSize:14.0f];
        [viewDetail addSubview:lblOption];            


        intOptionId = [[arrmOptionId objectAtIndex:j] intValue];

        if (intEventChoice == 1)
        {
            btnRadio = [[UIButton alloc]initWithFrame:CGRectMake(5, yLabel, 22, 22)];
            [btnRadio addTarget:self action:@selector(radioButtonPress:) forControlEvents:UIControlEventTouchUpInside];
            [btnRadio setImage:[UIImage imageNamed:@"btn_radio.png"] forState:UIControlStateNormal];
            [btnRadio setTag:intOptionId];
            [btnRadio setTitle:[NSString stringWithFormat:@"radio%d%d",intOptionId,intParamId] forState:UIControlStateNormal];
            [viewDetail addSubview:btnRadio];
        }
        else
        {
            btnCheckBox = [[UIButton alloc]initWithFrame:CGRectMake(5, yLabel, 22, 22)];
            [btnCheckBox setImage:[UIImage imageNamed:@"btn_checkbox.png"] forState:UIControlStateNormal];
            [btnCheckBox addTarget:self action:@selector(checkBoxButtonPress:) forControlEvents:UIControlEventTouchUpInside];
            [btnCheckBox setTag:intOptionId];
            [btnCheckBox setTitle:[NSString stringWithFormat:@"check%d,%d",intOptionId,intParamId] forState:UIControlStateNormal];
            [viewDetail addSubview:btnCheckBox];
        }

        yLabel = yLabel+ 21+10;
    }

So, my question is how to handle action on those buttons where buttons are created programmatically? and how to handle selection and deselection of buttons as those buttons are work like radio buttons and check box buttons. in case of radio button if i select one then other needs to be deselect and in case of check box, selection and deselection of check box needs to be managed.

I have tried with setting tag to buttons but its not working properly as i expect.

Please provide me some solution. Thanks in advance.


Solution

  • You have to declare your button inside your for loop. so each time loop run a new instance of button will generate.

    Create array to save buttons.

    NSMuttableDictionary *btnRadioDictionary = [NSMutableDictionary new];
    NSMuttableDictionary *btnCheckBoxDictionary= [NSMutableDictionary new];
    

    Set tag for each button inside loop

    for(int j = 0; j < nintOptionCount; j++)
    {
    UIButton *btnRadio;
    UIButton *btnCheckBox;
    // your other code
    
    
      btnRadio.tag = j; 
      btnCheckBox.tag = j;
    
    // save buttons to an array
    [btnRadioDictionary setValue:btnRadio forKey:j];
    [btnCheckBoxDictionary setValue:btnCheckBox forKey:j];
    }
    

    And identify the button clicked using tag in the IBAction

     -(IBAction) radioButtonPress:(id)sender 
     {
       // Write code Deselect all button here
       for(NSString *key in btnRadioDictionary)
       {
        UIButton *button =[btnRadioDictionary objectForKey:key];
        [button setImage:[UIImage imageNamed:@"btn_radio.png"] forState:UIControlStateNormal];
       }
       // Select required button
    
        UIButton *button =[btnRadioDictionary objectForKey:[sender tag]];
        [button setImage:[UIImage imageNamed:SELECTED_IMAGE_FOR_RADIO_BUTTON] forState:UIControlStateNormal];
    
       //Write separate action for each button if required.
    
       switch ([sender tag]) {
        case 0:
    
            break;
        case 1:
    
            break;
        case 2:
    
            break;
            /*
            .................
            */
        default:
            break;
       }
    }