0

I would like to know if somebody can please help me answer the following question. Case one of this code will ask for the name and you can click on one of the four options to chose the correct answer. I would like to know how can I do so I can type the answer to the question, for example:

What is my name?

My name is "Type answer here"

"Cesar"
"Karlos"
"William"
"Chiqui"

Here is my existing code:

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()
    }
}
Community
  • 1
  • 1
C.Sanchez
  • 25
  • 1
  • 7
  • 1
    So, you want them to type it in, rather than having buttons for the four buttons like in [your original question](http://stackoverflow.com/questions/37759585/)? Use `UITextField`. If you google "UITextField tutorial", you'll probably find many links. But if it's multiple choice, it seems that buttons might be a better UX. – Rob Jun 15 '16 at 04:01

1 Answers1

0

You can use UITextField to type answer for your question. But, it is not a good approach as you will have to handle all upper/lower case letter scenario. For e.g.- User can type name in the following way:

"Cesar", "cesar", "CESAR"

To avoid all these handling it would be better to use UIButton.

Ashish Verma
  • 1,776
  • 1
  • 16
  • 27
  • Dear Ashish, Thank you very much or your input. Can you do me a favor? If you are saying that it will be better to use an UIButton, Can you please give an example on how to do it? I will appreciate it. Regards, Cesar – C.Sanchez Jun 17 '16 at 03:16
  • The way you have already done by taking 4 buttons for answers – Ashish Verma Jun 17 '16 at 03:19