Mastering Development

How to call a value from a different ViewContoller? (Swift)

Iv created a quiz app, that tracks the user’s score till the user gets to the end. There is an if statement – when it reaches the end of the quiz, a results View controller pops up. My Question is I want to get the same score value presented at the end on the resultsViewContoller. I have connected it via segue and the identifier has been placed.

There must be a number of ways to do this other than using protocols to get the finalScore. (Protocols is what keeps coming up in google)

The user finishes the app. I call the segue method and the screen comes up

    func nextFruit() {
        if fruitness.fruitNumber == 0 {
        print(fruitness.fruitOptions[fruitness.fruitNumber]) //Only gets printed in the consol
        if fruitness.fruitNumber + 1 < fruitness.fruitOptions.count {
            fruitness.fruitNumber += 1
        } else {
            self.performSegue(withIdentifier: "goToResultsVC", sender: self)

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToResultsVC" {
            let destinationVC = segue.destination as! ResultsViewController
            destinationVC.finalScore = finalScore


Result View Controller

import UIKit

class ResultsViewController: UIViewController {

    var finalScore: Int?
    var fruitness = Fruitness()
    var toPass:String!
    @IBOutlet weak var finalScoreLabel: UILabel!
    override func viewDidLoad() {
        finalScoreLabel.text = "FINAL SCORE: \(fruitness.score)"
    override func prepare(for: UIStoryboardSegue, sender: Any?) {

    @IBAction func replayButton(_ sender: UIButton) {
        //self.dismiss(animated: true, completion: nil)

How can I get the same score on the results View Controller?

import Foundation

struct Fruitness
    var fruit = [Fruit]()
    var fruitOptions = [
        Fruit(id: 1, fruit:"🥝", name: "KIWI"),
        Fruit(id: 2, fruit:"🍎", name: "APPLE"),
        Fruit(id: 3, fruit:"🍐", name: "PEAR"),
        Fruit(id: 4, fruit:"🍊", name: "ORANGE"),
        Fruit(id: 5, fruit:"🍓", name: "STRAWBERRY"),
        Fruit(id: 6, fruit:"🍉", name: "WATERMELON"),
        Fruit(id: 7, fruit:"🍇", name: "GRAPES"),
        Fruit(id: 8, fruit:"🍌", name: "BANANA"),
        Fruit(id: 9, fruit:"🍒", name: "CHERRY")
    var fruitNumber = 0
    var score = 0
    public var finalScore = 0
    //    The score connected to the scoreLabel to keep track of the players score.
    func getScore() -> Int {
        return score
    //    The fruit names that get displayed on the label come straight from fruitOptions
    mutating func getFruitText() -> String {
        return fruitOptions[fruitNumber].name
    //    This has to return a float because thats what the progress view requires.
    func getProgress() -> Float {
        let progress = Float(fruitNumber) / Float(fruitOptions.count)
        return progress
    //    This lets you restart the game by restarting the variables and calling the method nextFruit.
    mutating func restartGame() {
        fruitNumber = 0
        score = 0
        //   nextFruit()


Leave a Reply

Your email address will not be published. Required fields are marked *