I do not have any experience with programing. I have done this looking at youtube videos for a couple of months. I will really appreciate if someone can please help me. When I run the code with the simulator it repeats the questions several time before the next new question is presented. I would like it to run so it presents one question without repeating the same question over and over. Below please find the 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()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Hide()
RamdomQuestions()
}
func RamdomQuestions () {
var RandomNumber = arc4random() % 4
RandomNumber += 1
switch (RandomNumber) {
case 1:
QuestionLabel.text = "Hola familia, Cual es mi nombre? "
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 = "Hola famili, cual es mi apellido? "
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 = "Quien hace la lachona mas rica? "
Button1.setTitle ("Willy", forState: UIControlState.Normal)
Button2.setTitle ("Mario", forState: UIControlState.Normal)
Button3.setTitle ("Karlos", forState: UIControlState.Normal)
Button4.setTitle ("Juan David", forState: UIControlState.Normal)
CorrectAnswer = "1"
break
case 4:
QuestionLabel.text = "Quien hace las tartas mas lindas"
Button1.setTitle ("Jili", forState: UIControlState.Normal)
Button2.setTitle ("Carvajal", forState: UIControlState.Normal)
Button3.setTitle ("Garcia", forState: UIControlState.Normal)
Button4.setTitle ("Leidy y Liz", forState: UIControlState.Normal)
CorrectAnswer = "4"
break
default:
break
}
}
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()
}
}
Try the following code, hope it will work for you
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.
Hide()
RamdomQuestions()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func RamdomQuestions () {
let randomIndex = Int(arc4random_uniform(UInt32(randomQuestionArray.count)))
if randomQuestionArray.count > 0 {
switch (randomQuestionArray[randomIndex]) {
case 1:
QuestionLabel.text = "Hola familia, Cual es mi nombre? "
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 = "Hola famili, cual es mi apellido? "
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 = "Quien hace la lachona mas rica? "
Button1.setTitle ("Willy", forState: UIControlState.Normal)
Button2.setTitle ("Mario", forState: UIControlState.Normal)
Button3.setTitle ("Karlos", forState: UIControlState.Normal)
Button4.setTitle ("Juan David", forState: UIControlState.Normal)
CorrectAnswer = "1"
break
case 4:
QuestionLabel.text = "Quien hace las tartas mas lindas"
Button1.setTitle ("Jili", forState: UIControlState.Normal)
Button2.setTitle ("Carvajal", forState: UIControlState.Normal)
Button3.setTitle ("Garcia", forState: UIControlState.Normal)
Button4.setTitle ("Leidy y Liz", 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()
}
}