Captain Code
Captain Code

Reputation: 277

How to save data between view controllers?

I am developing a quiz app and once you answer a question correctly the view controller segues to another view controller that informs the user they got the question correct and to click a button to continue. However when I run the simulator the data is not being saved once it segues to the next view controller. Once the user answer a question right i want the question to not be asked again. However as of now once you answer a question right and segue to the view controller that asks you continue one you press the button to segue back to the original view controller the user is asked questions they have already answered. How can I make it that the view controller that asks the user to continue to save the data from the original view controller to make sure the user isn't asked questions they've already answered correctly?

Here's my code for the original view controller:

import UIKit

class ViewController: UIViewController {

var questionList = [String]()

func randomQuestion() {



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



    }




    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 rightSeg() {

    performSegue(withIdentifier: "rightSeg", 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))


{
    print ("Correct!")
}

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

    wrongSeg()
print ("Wrong!")
    questionList = []
    }

    randomQuestion()

}

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


}




//variables
var seconds = 15
var timer = Timer()




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




}

Here's code to the view controller it segues to once you answer a question correctly:

import UIKit

class CorrectView: 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()

}

Upvotes: 1

Views: 700

Answers (2)

Hussain Shabbir
Hussain Shabbir

Reputation: 15035

Following are the ways where you can store data and pass to the viewController.:-

  • Create custom delegate and send data from one view controller to another. But it will not persist the data once you quit the app.

  • Second option is to create the singleton but do that only if it is
    required where you need to access the data anywhere from your code.

  • Third option is use UserDefaults or core-data but do that only if you want to persist

Upvotes: 2

rharding
rharding

Reputation: 571

If you want to save previously asked questions for only the current instantiation of the app, you could create a singleton. This involves creating a separate class with a field that contains the previously asked questions.

If you want to save previously asked questions even after the app has been terminated, use Core Data or User Defaults.

Upvotes: 0

Related Questions