ios - Adding contents of a label to contents of a button -
i have array questionhalf , next button invokes method nextquestionbutton cycles through items in questionhalf array , displays next string in label questionlabel
questionhalf = @[@"atlanta", @"detroit", @"houston", @"green bay", @"san francisco"]; which can cycled through using next button:
- (ibaction)nextquestionbutton:(id)sender; - (ibaction)nextquestionbutton:(id)sender { if (currentquestionindex < [questionhalf count] -1) currentquestionindex ++; else currentquestionindex = 0; _questionlabel.text = questionhalf [currentquestionindex]; } which displayed in label
@property (weak, nonatomic) iboutlet uilabel *questionlabel; i add 5 more buttons let users pick strings complete american football teams name.
when user picks need see if combined string valid team name.
i have array correctfullanswer contains valid full teams names , need test if users answer matches 1 of answers in string.
correctfullanswer = @[@"atlanta falcons", @"detroit lions", @"houston texans", @"green bay packers", @"san francisco 49'ers"]; i have added first of 5 required buttons
- (ibaction)packersanswerbutton:(id)sender; - (ibaction)packersanswerbutton:(id)sender { nsstring* selectedteamname = [nsstring stringwithformat:@"%@ %@", questionhalf [currentquestionindex], answerhalf]; if ([correctfullanswer indexofobject:selectedteamname] != nsnotfound) { [uiview animatewithduration:0.5 animations:^{ [packersanswerbutton.alpha setalpha:1]; } ];} } however getting error of undeclared identifier 'packersanswerbutton' when attempt button hide once correct answer has been selected.
just add 5 buttons view want add buttons. process similar following:
-(void)viewdidload { //i'm assuming containerview view going add buttons nsarray* teamnamesuffices = @[@"falcons", @"lions", @"texans", @"packers", @"49'ers"]; int = 0; for(nsstring* suffix in teamnamesuffices) { uibutton* btn = [uibutton buttonwithtype:uibuttontyperoundedrect]; btn.frame = cgrectmake(<%# required frame coordinates #>); btn.titlelabel.text = suffix; btn.tag = i; [btn addtarget:self action:@selector(suffixselected:) forcontrolevents:uicontroleventtouchupinside]; [containerview addsubview:btn]; i++; } } -(void)suffixselected:(uibutton*)sender { //here retrieve suffix text , compare if valid team name nsstring* suffix = sender.titlelabel.text; nsstring* selectedteamname = [nsstring stringwithformat:@"%@ %@",questionhalf [currentquestionindex]/* or _questionlabel.text, whichever accessible */,suffix]; if([correctfullanswer indexofobject:selectedteamname] != nsnotfound) { //a valid team name constructed [[[[uialertview alloc] initwithtitle:@"congratulations!" message:@"bingo!" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil] autorelease] show]; //or whatever required } } optionally can create suffix buttons designer(storyboard/xib), set titles accordingly , set targets suffixselected: method right their. in case need declare method (ibaction) rather (void).
please let me know if need explore else.
Comments
Post a Comment