I am creating a quiz app for ios for a big project at my school, but it is my first time making something with xcode and swift. I've run into a problem which i can't seem to figure out on my own. At the moment I am trying to keep score across multiple ViewControllers; I've tried a lot of different stuff, but it still doesn't work. I am, like I said, a noob to xcode and actually all coding.
At first i have the code for the score:
@IBOutlet weak var scoreLabel: UILabel!
var score = 0
In my app, when someone answers the right question, the score gets 1 point (score += 1
). After 4 questions of the first theme, 4 questions of a different theme, on a different ViewController are showed.
this is my code at the end of the first ViewController (MultipleChoice 2 is my second vc):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is MultipleChoice2 {
let vc = segue.destination as? MultipleChoice2
vc?.score = score
}
}
Code for my second vc:
var score = Int()
@IBOutlet weak var scoreLabel: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
scoreLabel?.text = String(score)
}
I might be doing this all wrong, but I expect the app to save the score from my first ViewController to my second, so that, when someone has score 3 points in the first vc, he still has 3 points in the second vc.
The other things that i have tried didn't work at all, but with this code i get the message Thread 1: signal SIGABRT, at class AppDelegate, after clicking on the button that sends me to the second viewcontroller.
I honestly have no clue on how to save my score to the next vc and it would be great if someone had a solution to this..