在测验应用中突出显示正确的答案

时间:2016-01-27 08:23:46

标签: ios swift

我是iOS开发的新手,并且一直在研究一些在线课程。在其中一个中我们开发了一个测验应用程序,我希望通过改进应用程序来提高我的技能。

该应用使用.json文件作为'数据库'的问题和答案。这个.json文件如下所示......

  {
    "id" : "1",
    "question": "Earth is a:",
         "answers": [
        "Planet",
        "Meteor",
        "Star",
        "Asteroid"
      ],
      "difficulty": "1"
  }

......只是继续讨论500多个问题。

目前,该应用向用户提供了四个可能答案的问题。在.json文件中,第一个答案始终是正确答案,但应用程序被编码为随机播放答案,以便始终不会列出正确的答案。

该应用程序也经过编码,以便显示答案的四个按钮(我使用四个不同颜色的按钮图像)被禁用,并且在用户进行选择后也会在外观上变暗,除了他们选择的按钮没有变暗以便突出他们的选择。

我想做的是改变这一点,以便突出显示具有正确答案的按钮,作为通知用户正确答案是什么的方式。

我使用以下代码加载问题和答案并随机播放答案:

 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 as String

    let indices : [Int] = [0,1,2,3]
    //let newSequence = shuffle(indices)
    let newSequence = indices.shuffle()
    var i : Int = 0
    for(i = 0; i < newSequence.count; i++)
    {
        let index = newSequence[i]
        if(index == 0)
        {
            // we need to store the correct answer index
            currentCorrectAnswerIndex =  i

        }

        let answer = arr.objectAtIndex(index) as! NSString
        switch(i)
        {
        case 0:
            buttonA.setTitle(answer as String, forState: UIControlState.Normal)
            break;

        case 1:
            buttonB.setTitle(answer as String, forState: UIControlState.Normal)
            break;

        case 2:
            buttonC.setTitle(answer as String, forState: UIControlState.Normal)
            break;

        case 3:
            buttonD.setTitle(answer as String, forState: UIControlState.Normal)
            break;

        default:
            break;
        }

目前我正在使用此代码检查答案:

var currentCorrectAnswerIndex : Int = 0



    func checkAnswer( answerNumber : Int)
    {
        if(answerNumber == currentCorrectAnswerIndex)
        {
            // we have the correct answer
            labelFeedback.text = "Correct! +1"
            labelFeedback.textColor = UIColor.greenColor()
            score = score + 1
            labelScore.text = "score: \(score)"
            SaveScore()
            // later we want to play a "correct" sound effect
            PlaySoundCorrect()

        }
        else
        {
            // we have the wrong answer
            labelFeedback.text = "Wrong answer"
            labelFeedback.textColor = UIColor.redColor()
            // we want to play a "incorrect" sound effect
            PlaySoundWrong()
        }

在突出显示按钮时,我将使用编码,例如:

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 A 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)
}

我能解决的问题是如何对应用进行编码以突出正确的答案?目前,上面的代码有效地突出了通过调暗“其他”字母的α来按下的按钮。纽扣。如何让应用确定哪个&#39;洗牌&#39;按钮包含正确答案并突出显示该答案?

1 个答案:

答案 0 :(得分:0)

滚动骰子时,请记住包含正确答案的按钮的索引。

此外,您可以将所有按钮放在一个数组中,并使用forall循环操作所有按钮。这使得代码变得更加清晰,并且摆脱了奇怪的变量名称和切换案例。