iphone - IBAction used with Dynamically added UIbuttons -
good evening all!
i have uibuttons added dynamically view , of course have ibaction handles button events.
the problem is: how can detect button pressed if thing know (id)sender , array of buttons?
buttons never same, every button has different behavior. when want use static buttons , connect them through ib use :
-(ibaction)dosomething:(id)sender { if(sender == button1) dosomething; if(sender == button2) dosomething else; if(sender == button3) dosomething3; }
in case not work because there no button1, button2, button3 mutablearray of buttons have same name allocated it. button!
i tried using way above no success , tried getting tag of button have nothing compare to!
i appreciate help.
sincerely
l_sonic ps dynamicaly means creating buttons in random time during run time this
-(void)configactionsheetview { buttonview = [[uiview alloc]initwithframe:cgrectmake(0.0,460, 60, 480)]; [buttonview setbackgroundcolor:[uicolor blackcolor]]; [buttonview setalpha:0.6]; (int = 0 ;i<[buffbuttons count];i++) { uibutton *custombutton = [buffbuttons objectatindex:i]; custombutton = [uibutton buttonwithtype:uibuttontyperoundedrect]; //uilabel *customlabel = [[uilabel alloc]init]; //[custombutton settag:(i)+11]; [custombutton addtarget:self action:@selector(activatebuffeffect:) forcontrolevents:uicontroleventtouchupinside]; [custombutton setalpha:1.0]; custombutton.frame = cgrectmake(8.0, 5+(50*i), 44.0, 44.0); [custombutton settitle:nil forstate:uicontrolstatenormal]; buttonview.frame = cgrectmake(0, 460, 60, 50+(44*(i+1))); [buttonview addsubview:custombutton]; } }
this inside functions , gets called during run time. buffbuttons mutablearray buttons gets populated during runtime. need solution cannot different eventhandling method everybutton.
when "added dynamically" assume mean created piece of code. since buttons different things , know button should do, why don't add different actions different buttons?
uibutton *mycreatedbutton = [[uibutton alloc] init]; [mycreatedbutton addtarget:self action:@selector(dosomething:) forcontrolevents:uicontroleventtouchupinside]; uibutton *myothercreatedbutton = [[uibutton alloc] init]; [myothercreatedbutton addtarget:self action:@selector(dosomethingelse:) forcontrolevents:uicontroleventtouchupinside];
in above code target (set self) class method want run found, action method want run , controlevent should cause method run.
if did split code in different methods these (you not need specify them in header):
-(void)dosomething:(id)sender { // somthing here ... } -(void)dosomethingelse:(id)sender { // somthing else here ... }
this way don't need know button pressed since correct code called anyway. besides makes cleaner if need change code of buttons.
Comments
Post a Comment