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


i have created radio , check box buttons programmatically according web service response, number of buttons varies.

following code create 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, question how handle action on buttons buttons created programmatically? , how handle selection , deselection of buttons buttons work radio buttons , check box buttons. in case of radio button if select 1 other needs deselect , in case of check box, selection , deselection of check box needs managed.

i have tried setting tag buttons not working expect.

please provide me solution. in advance.

you have declare button inside loop. each time loop run new instance of button generate.

create array save buttons.

nsmuttabledictionary *btnradiodictionary = [nsmutabledictionary new]; nsmuttabledictionary *btncheckboxdictionary= [nsmutabledictionary new]; 

set tag each button inside loop

for(int j = 0; j < nintoptioncount; j++) { uibutton *btnradio; uibutton *btncheckbox; // other code     btnradio.tag = j;    btncheckbox.tag = j;  // save buttons array [btnradiodictionary setvalue:btnradio forkey:j]; [btncheckboxdictionary setvalue:btncheckbox forkey:j]; } 

and identify button clicked using tag in ibaction

 -(ibaction) radiobuttonpress:(id)sender   {    // write code deselect 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 each button if required.     switch ([sender tag]) {     case 0:          break;     case 1:          break;     case 2:          break;         /*         .................         */     default:         break;    } } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -