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