2016-06-15 10 views
0

誰かが私に次の質問に答えるのを手伝ってもらえますか?このコードのケース1で名前を尋ね、4つのオプションのいずれかをクリックして正しい答えを選択できます。私はどのようにすればよいのかを知りたいので、質問に対する答えを入力することができます。回答で多肢選択式スイフト

私の名前は何ですか?

私の名前は、 "ここにタイプの回答"

"シーザー"
"Karlos"
"ウィリアム"
"チキ" ここ

がある私の既存のコードです:

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

[あなたの元の質問](http://stackoverflow.com/questions/37759585/)のように4つのボタンのボタンを押すのではなく、それらを入力する必要がありますか? 'UITextField'を使います。あなたがGoogleの "UITextFieldチュートリアル"の場合は、おそらく多くのリンクを見つけるでしょう。しかし、それが複数の選択肢であれば、ボタンがより優れたUXであるように思えます。 – Rob

答えて

0

UITextFieldを入力すると、質問に答えを入力できます。しかし、すべての大文字/小文字のシナリオを処理する必要があるため、良いアプローチではありません。 UIButtonを使用する方が良いでしょう取り扱い、これらすべてを避けるために

"Cesar", "cesar", "CESAR" 

:e.g.-ユーザにとって は次のように名前を入力することができます。

+0

親愛なるAshishさん、ありがとうございました。ちょっとお願いできますか?あなたがUIButtonを使う方が良いと言っているのなら、それを行う方法の例を教えてください。私はそれを感謝します。よろしく、セザール –

+0

回答は4つのボタンを取って –

関連する問題