ios - Highlighting the correct answer in a quiz app -


i'm new ios development , have been working through online courses. in 1 of these develop quiz app , i'd improve skills improving app beyond covered in course.

the app uses .json file 'database' of questions , answers. .json file looks follows...

  {     "id" : "1",     "question": "earth a:",          "answers": [         "planet",         "meteor",         "star",         "asteroid"       ],       "difficulty": "1"   } 

...and keeps going on 500 questions.

at present, app presents user question 4 possible answers. in .json file, first answer correct answer, app coded shuffle answers correct answer not listed first.

the app coded 4 buttons (i use 4 different coloured images buttons) displaying answers disabled , dimmed in appearance after user makes selection, except button selected not dimmed choice highlighted.

what change button correct answer highlighted instead, way of notifying user correct answer was.

i'm using following code load questions , answers , shuffle answers:

 func loadallquestionsandanswers()     {         let path = nsbundle.mainbundle().pathforresource("content", oftype: "json")         let jsondata : nsdata = nsdata(contentsoffile: path!)!         allentries = (try! nsjsonserialization.jsonobjectwithdata(jsondata, options: nsjsonreadingoptions.mutablecontainers)) as! nsarray         //println(allentries)      }   func loadquestion(index : int) {     let entry : nsdictionary = allentries.objectatindex(index) as! nsdictionary     let question : nsstring = entry.objectforkey("question") as! nsstring     let arr : nsmutablearray = entry.objectforkey("answers") as! nsmutablearray      //println(question)     //println(arr)      labelquestion.text = question string      let indices : [int] = [0,1,2,3]     //let newsequence = shuffle(indices)     let newsequence = indices.shuffle()     var : int = 0     for(i = 0; < newsequence.count; i++)     {         let index = newsequence[i]         if(index == 0)         {             // need store correct answer index             currentcorrectanswerindex =           }          let answer = arr.objectatindex(index) as! nsstring         switch(i)         {         case 0:             buttona.settitle(answer string, forstate: uicontrolstate.normal)             break;          case 1:             buttonb.settitle(answer string, forstate: uicontrolstate.normal)             break;          case 2:             buttonc.settitle(answer string, forstate: uicontrolstate.normal)             break;          case 3:             buttond.settitle(answer string, forstate: uicontrolstate.normal)             break;          default:             break;         } 

at present using code check answer:

var currentcorrectanswerindex : int = 0        func checkanswer( answernumber : int)     {         if(answernumber == currentcorrectanswerindex)         {             // have correct answer             labelfeedback.text = "correct! +1"             labelfeedback.textcolor = uicolor.greencolor()             score = score + 1             labelscore.text = "score: \(score)"             savescore()             // later want play "correct" sound effect             playsoundcorrect()          }         else         {             // have wrong answer             labelfeedback.text = "wrong answer"             labelfeedback.textcolor = uicolor.redcolor()             // want play "incorrect" sound effect             playsoundwrong()         } 

in terms of highlighting buttons going use coding such as:

func resetanswerbuttons() {     buttona.alpha = 1.0     buttonb.alpha = 1.0     buttonc.alpha = 1.0     buttond.alpha = 1.0     buttona.enabled = true     buttonb.enabled = true     buttonc.enabled = true     buttond.enabled = true }  @ibaction func pressedbuttona(sender: uibutton) {     print("button pressed")     buttonb.alpha = 0.3     buttonc.alpha = 0.3     buttond.alpha = 0.3      buttona.enabled = false     buttonb.enabled = false     buttonc.enabled = false     buttond.enabled = false      checkanswer(0) } @ibaction func pressedbuttonb(sender: uibutton) {     print("button b pressed")     buttona.alpha = 0.3     buttonc.alpha = 0.3     buttond.alpha = 0.3      buttona.enabled = false     buttonb.enabled = false     buttonc.enabled = false     buttond.enabled = false     checkanswer(1) }  @ibaction func pressedbuttonc(sender: uibutton) {     print("button c pressed")     buttona.alpha = 0.3     buttonb.alpha = 0.3     buttond.alpha = 0.3      buttona.enabled = false     buttonb.enabled = false     buttonc.enabled = false     buttond.enabled = false     checkanswer(2) }  @ibaction func pressedbuttond(sender: uibutton) {     print("button d pressed")     buttona.alpha = 0.3     buttonb.alpha = 0.3     buttonc.alpha = 0.3      buttona.enabled = false     buttonb.enabled = false     buttonc.enabled = false     buttond.enabled = false     checkanswer(3) } 

what can't head around how code app highlights correct answer? @ present, above code highlights button pressed dimming alpha of 'other' buttons. how app identify 'shuffled' button contains correct answer , highlight 1 instead?

when rolling dice, remember index of button containing right answer.

also, may put buttons in array , manipulate of them forall loops. makes code much, cleaner , gets rid of weird variable names , switch case.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -