0

Im creating a quiz app to where once you answer a question correctly it segues to another view controller where there is a label that says "correct answer!" and a contains a button that label reads "Continue?". One you press the button it sends you back to the original view controller where the questions are asked. How ever once you segue from the viewcontroller with the "Continue?" button back to the view controller that asks the questions the data is reset and the user is asked questions they have already answered. How could I make it to where the "continue?" button view controller keeps track of the completed questions so they are not asked again in the question view controller? I am new to swift so would I use a delegate or protocol? Or are those things the same? Ive correctly made it segue from the question view controller to the "continue" view controller however I want the data to stay updated once I segue back to the question view controller so no questions that have already been answered are asked again.

Here is my code from the question view controller:

Import UIKit

class ViewController: UIViewController {

    var questionList = [String]()


func updateCounter() {

    counter -= 1
    questionTimer.text = String(counter)

    if counter == 0 {


        timer.invalidate()
        wrongSeg()
        counter = 15

    }



}


func randomQuestion() {



    //random question
    if questionList.isEmpty {
        questionList = Array(QADictionary.keys)

        questionTimer.text = String(counter)




    }






    let rand = Int(arc4random_uniform(UInt32(questionList.count)))
    questionLabel.text = questionList[rand]


    //matching answer values to go with question keys
    var choices = QADictionary[questionList[rand]]!

      questionList.remove(at: rand)



    //create button
        var button:UIButton = UIButton()

    //variables
    var x = 1
    rightAnswerBox = arc4random_uniform(4)+1


        for index in 1...4
        {



            button = view.viewWithTag(index) as! UIButton

            if (index == Int(rightAnswerBox))
            {
                button.setTitle(choices[0], for: .normal)

            }

            else {
                button.setTitle(choices[x], for: .normal)
                x += 1

            }


            randomImage()

        }
    }


let QADictionary = ["Who is Thor's brother?" : ["Atum", "Loki", "Red Norvell", "Kevin Masterson"], "What is the name of Thor's hammer?" : ["Mjolinr", "Uru", "Stormbreaker", "Thundara"], "Who is the father of Thor?" : ["Odin", "Sif", "Heimdall", "Balder"]]



//wrong view segue
func wrongSeg() {

   performSegue(withIdentifier: "wrongViewSegue", sender: self)

}

//proceed screen
func continueSeg() {

    performSegue(withIdentifier: "continueSeg", sender: self)
}

//variables
var rightAnswerBox:UInt32 = 0
var index = 0



//Question Label
@IBOutlet weak var questionLabel: UILabel!

//Answer Button
@IBAction func buttonAction(_ sender: AnyObject) {

if (sender.tag == Int(rightAnswerBox))


{
    counter = 15
    questionTimer.text = String(counter)
    print ("Correct!")
    continueSeg()
}

else if (sender.tag != Int(rightAnswerBox)) {

    wrongSeg()
print ("Wrong!")
    timer.invalidate()
    questionList = []

    }

    randomQuestion()

}

override func viewDidAppear(_ animated: Bool)
{
randomQuestion()


}



//variables
var counter = 15

var timer = Timer()

@IBOutlet weak var questionTimer: UILabel!

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

    timer = Timer.scheduledTimer(timeInterval: 1, target:self, selector: #selector(ViewController.updateCounter), userInfo: nil, repeats: true)


}

Here is my code for the "continue" view controller (all it contains right now is a segue back to the question view controller but I want it to keep track of which questions have already been answered by storing the data from the array of questions in the question view controller):

import UIKit

class ContinueView: UIViewController {


//correct answer label
@IBOutlet weak var correctLbl: UILabel!

//background photo
@IBOutlet weak var backgroundImage: UIImageView!

func backToQuiz() {

    performSegue(withIdentifier: "continueSeg", sender: self)
}

@IBAction func `continue`(_ sender: Any) {

    backToQuiz()
}



override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
Captain Code
  • 277
  • 1
  • 3
  • 13
  • use counter that will direct your app to show which question will be called from array. like you have 10 question in Array and user had answered 5 make counter count equal to 5 and when you navigate back from segue to questions screen call QuestionArray[Count + 1].. make sure your count starts with 0 and when you call questiioArray it will call Question next to count value. in start count is 0 so call questionsArray[count + 1] – iOS Geek Jul 24 '17 at 05:02

1 Answers1

0

Move your call to randomQuestion from viewDidAppear to viewDidLoad. The viewDidLoad only called once, but viewDidAppear will always be called every time the controller is shown.

Also take note that Apple says

If a view controller is presented by a view controller inside of a popover, this method is not invoked on the presenting view controller after the presented controller is dismissed.

Take a look on discussion about the differences

xmhafiz
  • 3,482
  • 1
  • 18
  • 26