eg. super Guess < Next button operation optimization >
Total Effect:
1 #pragmaMark's next question.2-(ibaction) nextquestion3 {4 //1. Index of current answer5self.index++;6 //2. Remove the problem model data from the array by index7Hmquestion *question =Self.questions[self.index];8 9 //3. Setting up basic informationTen [self setupbasicinfo:question]; One //4. Set Answer button A //Remove all buttons from the answer area first - [self creatanswerbuttons:question]; - //5. Set the option button the [self createoptionbuttons:question]; - } - - + /** Set BASIC information*/ --(void) Setupbasicinfo: (Hmquestion *) Question + { ASelf.noLabel.text = [NSString stringWithFormat:@"%d/%d", Self.index +1, Self.questions.count]; at -Self.titleLable.text =Question.title; - [Self.iconbutton setimage:[uiimage ImageNamed:question.icon] forstate:uicontrolstatenormal]; - //If you reach the next question, disable the next day cow -self.nextQuestionButton.enabled = (Self.index <self.questions.count-1); - in - to } + - /** Create an Answer area button*/ the *-(void) Creatanswerbuttons: (Hmquestion *) Question $ {Panax Notoginseng for(UIView *btninchSelf.answerView.subviews) {//UIButton *btn in Self.answerView.subviews all UI controls inherit from UIView - [btn Removefromsuperview]; the + } ACGFloat answerw =Self.answerView.bounds.size.width; the intLenght =question.answer.length; +CGFloat Answerx = (answerw-kbuttonwidth *lenght-kbuttonmargin * (Lenght-1))*0.5; - //Create all answer buttons $ for(inti =0; I < lenght; i++) { $CGFloat x = Answerx +i* (kbuttonmargin +kbuttonwidth); -UIButton *btn = [[UIButton alloc] Initwithframe:cgrectmake (x,0, Kbuttonwidth, Kbuttonheight)]; - //btn.backgroundcolor =[uicolor Whitecolor]; the[Btn setbackgroundimage:[uiimage imagenamed:@"Btn_answer"] forstate:uicontrolstatenormal]; -[Btn setbackgroundimage:[uiimage imagenamed:@"btn_answer_highlighted"]forstate:uicontrolstatehighlighted];Wuyi the [Self.answerview addsubview:btn]; - } Wu - About $ - } - - /** Selected Button*/ A-(void) Createoptionbuttons: (Hmquestion *) Question + { the for(UIView *viewinchself.optionsView.subviews) { - [view Removefromsuperview]; $ } theCGFloat optionw =Self.optionsView.bounds.size.width; theCGFloat Optionx = (Optionw-ktotolcol * kbuttonwidth-(Ktotolcol-1) *kbuttonmargin) *0.5; the for(inti =0; I <question.options.count; i++) { the intRow =i/ktotolcol;//Line - intcol = I%ktotolcol;//column inCGFloat x = Optionx +col * (Kbuttonmargin +kbuttonwidth); theCGFloat y = row * (Kbuttonmargin +kbuttonheight); theUIButton *btn =[[UIButton alloc]initwithframe:cgrectmake (x, Y, Kbuttonwidth, kbuttonheight)]; About the the //btn.backgroundcolor =[uicolor Whitecolor]; the[Btn setbackgroundimage:[uiimage imagenamed:@"btn_option"] forstate:uicontrolstatenormal]; +[Btn setbackgroundimage:[uiimage imagenamed:@"btn_option_highlighted"] forstate:uicontrolstatehighlighted]; - //Set Alternate answers the [btn Settitle:question.options[i] forstate:uicontrolstatenormal];Bayi [btn Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal]; the the [Self.optionsview addsubview:btn]; - - } the the}
Code Optimization---Main method as concise as possible