-1

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..

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Jelle
  • 1

3 Answers3

0

Check if all the outlets are connected. After that check whether segue is connected or not.

Change

var score = Int()

to

var score: Int = 0

in secondVC and try again.

Rahul Dasgupta
  • 894
  • 6
  • 18
0

Try following and let me know if you face any trouble getting it working : First of all change declaration of score variable in secondVC to

var score : Int = 0

then in prepare for segue method you can try these : first set segue identifier from storyboard as it is more convenient to use : then you can check if we are in proper condition by

if segue.identifier == "showSecondVC" {
    let vc = segue.destination as! MultipleChoice2
    vc.score = score
}

Also use breakpoint to check whether everything is executing properly One more thing is you can set label text by using following code also :

scoreLabel?.text = "\(score)"
Umang Loriya
  • 840
  • 8
  • 15
0
  1. Change var score = Int() to var score: Int? in second view controller.
  2. Set segue identifier to second view controller in storyboard.
  3. Modify segue method as below in first view controller

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showSecondVC" {
            let vc = segue.destination as! MultipleChoice2
            vc.score? = score
        }
    }
    
RJ168
  • 1,006
  • 2
  • 12
  • 22