Creating Backup area buttons and judging results
/** Create a reserve selection button*/- (void) Createoptionbuttons: (Hmquestion *) question{//Issue: 21 buttons are recreated each time the next method is called//FIX: If the button already exists and is 21, just change the button title to if(Self.optionsView.subviews.count! =question.options.count) {//re-create all buttons for(UIView *viewinchself.optionsView.subviews) {[View Removefromsuperview]; } cgfloat optionw=Self.optionsView.bounds.size.width; CGFloat Optionx= (Optionw-ktotolcol * kbuttonwidth-(Ktotolcol-1) * kbuttonmargin) *0.5; for(inti =0; i < Question.options.count; i++) { introw = I/Ktotolcol; intCol = i%Ktotolcol; CGFloat x= Optionx + col * (Kbuttonmargin +kbuttonwidth); CGFloat y= Row * (Kbuttonmargin +kbuttonheight); UIButton*BTN =[[UIButton alloc] Initwithframe:cgrectmake (x, Y, Kbuttonwidth, kbuttonheight)]; [Btn setbackgroundimage:[uiimage imagenamed:@"btn_option"] Forstate:uicontrolstatenormal]; [Btn setbackgroundimage:[uiimage imagenamed:@"btn_option_highlighted"] forstate:uicontrolstatehighlighted]; [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; [Self.optionsview ADDSUBVIEW:BTN]; //Add a button listener method[btn addtarget:self Action: @selector (Optionclick:) forcontrolevents:uicontroleventtouchupinside]; } NSLog (@"Create candidate Button"); } //if the button already exists, when you click on the next question, just set the title to inti =0; for(UIButton *btninchself.optionsView.subviews) {//Set Alternate answers[Btn settitle:question.options[i++] Forstate:uicontrolstatenormal]; //reply to the hidden state of all buttonsBtn.hidden =NO; // //Add a button listener method//tip, if you add a listening method again, it means that the listener will be added every time the next question is called.//[btn addtarget:self Action: @selector (Optionclick:) forcontrolevents:uicontroleventtouchupinside]; }}#pragmaMark-Candidate Button click Method/** Candidate button click*/- (void) Optionclick: (UIButton *) button{//1. Find the first blank text button in the answer areaUIButton *btn =[self Firstanswerbutton]; //if no button is found, return directly if(btn = = nil)return; //2. Set the button's caption to the buttons in the answer area[btn SetTitle:button.currentTitle forstate:uicontrolstatenormal]; //3. Hide the buttonButton.hidden =YES; //4. Judging the results[self judge];}/** Judging Results*/- (void) judge{//if four buttons have text, you need to determine the result//buttons to traverse all the answer areasBOOL Isfull =YES; Nsmutablestring*STRM = [nsmutablestringstring]; for(UIButton *btninchself.answerView.subviews) {if(Btn.currentTitle.length = =0) { //as long as there is a button without a wordIsfull =NO; Break; } Else { //There are words, stitching temporary strings[StrM AppendString:btn.currentTitle]; } } if(isfull) {NSLog (@"they have words ."); //determine if the answer is the same//get the current question according to Self.indexHmquestion *question =Self.questions[self.index]; //if consistent, go to the next question if([StrM isEqualToString:question.answer]) {NSLog (@"That 's correct."); [Self Setanswerbuttonscolor:[uicolor bluecolor]]; //wait 0.5 seconds to enter the next question[Self performselector: @selector (nextquestion) Withobject:nil Afterdelay:0.5]; } Else { //if inconsistent, modify the button text color to prompt the userNSLog (@"wrong answer."); [Self Setanswerbuttonscolor:[uicolor redcolor]]; } }}/** Modify the color of the answer area button*/- (void) Setanswerbuttonscolor: (Uicolor *) color{ for(UIButton *btninchself.answerView.subviews) {[btn settitlecolor:color forstate:uicontrolstatenormal]; }}//find the first blank text button in the answer area-(UIButton *) firstanswerbutton{//take the caption of the button//traverse the answer area all buttons for(UIButton *btninchself.answerView.subviews) {if(Btn.currentTitle.length = =0) { returnbtn; } } returnNil;}
iOS fourth day (5: Create Backup Area button and determine results)