iphone - How to create Multiple buttons programatically, get button name whenever press on particur button? -
i want create multiple buttons using mutablearray count. after creating button, whenever pressing on particular button, want display button name in label. once press on button, showing mutablearray last index value name. please in problem. fresher ti ios. code. thank in advance.
-(void)viewdidload { nsmutablearray *mute_array=[[nsmutablearray alloc]initwithobjects:@"one", @"two", @"three", @"four", @"five", nil]; (int k=0; k<[mute_array count]; k++){ uibutton *btn_sub_category = [uibutton buttonwithtype:uibuttontyperoundedrect]; [btn_sub_category addtarget:self action:@selector(clicks:) forcontrolevents:uicontroleventtouchdown]; [btn_sub_category settitle:[mute_sub_category objectatindex:k] forstate:uicontrolstatenormal]; btn_sub_category.frame = cgrectmake(278, k*130, 483, 44); [self.view addsubview:btn_sub_category]; } } -(void)clicks:(id)sender{ label.text=[btn_sub_category currenttitle]; //here want send name of clicked button current title }
but, here button current title showing mutablearray last object "five" only. want pressed button current title. thank you.
-(void)clicks:(id)sender{ uibutton *thebutton = (uibutton*)sender; nsstring *thebuttontitle = [thebutton titleforstate:uicontrolstatenormal] nslog(@"thebuttontitle :- %@", thebuttontitle); label.text=thebuttontitle; //here want send name of clicked button current title }
try code. using following method title of button
titleforstate:
returns title associated specified state. - (nsstring *)titleforstate:(uicontrolstate)state parameters
state
the state uses title. possible values described in uicontrolstate.
return value
the title specified state. if no title has been set specific state, method returns title associated uicontrolstatenormal state.
read documentation https://developer.apple.com/library/ios/documentation/uikit/reference/uibutton_class/uibutton/uibutton.html#//apple_ref/occ/instm/uibutton/titleforstate:
Comments
Post a Comment