Swift多选类型的答案

时间:2016-06-15 03:34:29

标签: ios swift

我想知道是否有人可以帮我回答以下问题。此代码中的第一个代码将询问名称,您可以单击四个选项之一来选择正确的答案。我想知道我该怎么做才能输入问题的答案,例如:

  

我的名字是什么?

     

我的名字是“在这里输入答案”

     

“塞萨尔”
  “卡洛斯”
  “威廉”
  “Chiqui酒店”

这是我现有的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var QuestionLabel: UILabel!
    @IBOutlet weak var Button1: UIButton!
    @IBOutlet weak var Button2: UIButton!
    @IBOutlet weak var Button3: UIButton!
    @IBOutlet weak var Button4: UIButton!
    @IBOutlet weak var Next: UIButton!
    @IBOutlet weak var LabelEnd: UILabel!

    var CorrectAnswer = String()
    var randomQuestionArray: [Int] = [1, 2, 3, 4]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        RamdomQuestions()
            }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func RamdomQuestions () {
        let randomIndex = Int(arc4random_uniform(UInt32(randomQuestionArray.count)))

        switch (randomQuestionArray[randomIndex]) {

        case 1:
            QuestionLabel.text = "What is my name? "
            Button1.setTitle ("Cesar", forState: UIControlState.Normal)
            Button2.setTitle ("Karlos", forState: UIControlState.Normal)
            Button3.setTitle ("William", forState: UIControlState.Normal)
            Button4.setTitle ("Chiqui", forState: UIControlState.Normal)
            CorrectAnswer = "2"

            break
        case 2:
            QuestionLabel.text = "What is my last name? "
            Button1.setTitle ("Perez", forState: UIControlState.Normal)
            Button2.setTitle ("Carvajal", forState: UIControlState.Normal)
            Button3.setTitle ("Garcia", forState: UIControlState.Normal)
            Button4.setTitle ("Sanchez", forState: UIControlState.Normal)
            CorrectAnswer = "1"
            break
        case 3:
            QuestionLabel.text = "What is my favorite dish? "
            Button1.setTitle ("Pasta", forState: UIControlState.Normal)
            Button2.setTitle ("Fish", forState: UIControlState.Normal)
            Button3.setTitle ("Vegetables", forState: UIControlState.Normal)
            Button4.setTitle ("McMc", forState: UIControlState.Normal)
            CorrectAnswer = "1"

            break
        case 4:
            QuestionLabel.text = "What is my favorite color"
            Button1.setTitle ("Red", forState: UIControlState.Normal)
            Button2.setTitle ("Blue", forState: UIControlState.Normal)
            Button3.setTitle ("Orange", forState: UIControlState.Normal)
            Button4.setTitle ("Black", forState: UIControlState.Normal)
            CorrectAnswer = "4"


            break

        default:
            break
        }
       randomQuestionArray.removeAtIndex(randomIndex)

    }
    func Hide (){
        LabelEnd.hidden = true
        Next.hidden = true
    }
    func UnHide () {
        LabelEnd.hidden = false
        Next.hidden = false
    }

    @IBAction func Button1Action(sender: AnyObject) {
        UnHide()
        if (CorrectAnswer == "1") {
            LabelEnd.text = "Correcto"
        }
        else{
        LabelEnd.text = "Falso"

            }
    }
     func Button2Action(sender: AnyObject) {
        UnHide()
        if (CorrectAnswer == "2") {
            LabelEnd.text = "Correcto"

    }
        else{
            LabelEnd.text = "Falso"

        }
    }

    func Button3Action(sender: AnyObject) {
        UnHide()
        if (CorrectAnswer == "3") {
            LabelEnd.text = "Correcto"

        }
        else{
           LabelEnd.text = "Falso"

    }
    }

     func Button4Action(sender: AnyObject) {
        UnHide()
            if (CorrectAnswer == "4") {
            LabelEnd.text = "Correcto"

        }

        else{
            LabelEnd.text = "Falso"
    }
    }
    @IBAction func Next(sender: AnyObject) {
        RamdomQuestions()
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用*为您的问题键入答案。但是,这不是一个好方法,因为你必须处理所有大写/小写字母方案。 例如,用户可以通过以下方式输入姓名:

UITextField

为了避免所有这些处理,最好使用UIButton。