0

I have mulitplie View Controllers within an app which represent different levels within a game..

ViewController.swift SecondViewController.Swift etc

On each of the different levels there is a several buttons, 1 which is the correct answer and 2 which are incorrect. on each view controller i also have labels that update the score of the user within the button...

@IBAction func buttonCorrect(_ sender: UIButton)
{
cScore +=1
aScore +=1

correctScore.text = NSString(format: "%i", cScore) as String
attemptScore.text = NSString(format: "%i", aScore) as String
}

@IBAction func buttonIncorrect(_ sender: UIButton)
{
cScore +=0
aScore +=1
correctScore.text = NSString(format: "%i", cScore) as String
attemptScore.text = NSString(format: "%i", aScore) as String
}

How do i save the data/ number produced by this level and carry it over to the SecondViewController.swift?

The aim is to be able to save the data at the end of the game and use it to plot a chart of how many attempts the user had at the question vs the actual amount of correct scores they had?

2 Answers2

0

Assuming you are using Storyboard for your layout, I have written a possible solution below. Basically, you keep a running total of aScore and cScore and pass the running totals to the next view controller's aScore and cScore variables using prepareForSegue. I have created a function called proceedToNextLevel which is called whenever you want to proceed to the next level.

You level 1 View Controller would look something like this:

    class Level1ViewController: UIViewController {
        var cScore = 0
        var aScore = 0

        //call this function when you want to proceed to level 2, connect a segue between your level 1 view controller and level 2 view controller give the segue an identifier "ProceedToLevel2Segue"
        func proceedToNextLevel() {
             performSegue(withIdentifier: "ProceedToLevel2Segue", sender: nil)
        }

        //pass the accumulated aScore and cScore for level 1 to level 2's aScore and cScore
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            let destinationVC = segue.destination as! Level2ViewController

            destinationVC.cScore = self.cScore
            destinationVC.aScore = self.aScore

        }

         @IBAction func buttonCorrect(_ sender: UIButton)
         {
         cScore +=1
         aScore +=1

         correctScore.text = NSString(format: "%i", cScore) as String
         attemptScore.text = NSString(format: "%i", aScore) as String
         }

        @IBAction func buttonIncorrect(_ sender: UIButton)
        {
        cScore +=0
        aScore +=1
        correctScore.text = NSString(format: "%i", cScore) as String
        attemptScore.text = NSString(format: "%i", aScore) as String
        }


    }

Your level 2 view controller's aScore and cScore will be initialized by the prepareForSegue method in the Level1ViewController to the totals you had in Level 1. If they are not initialized they default to the values of zero given in Level 2 View Controller. I have kept the logic that increments aScore and cScore the same in the level 2 view controller.

    class Level2ViewController: UIViewController {

        //these will be initialized to the running total of aScore and cScore from level 1
        var cScore: Int!
        var aScore: Int!

        //proceed to level 3
        func proceedToNextLevel() {
             performSegue(withIdentifier: "ProceedToLevel3Segue", sender: nil)
        }

        //the running total for aScore and cScore is passed to level 3
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            let destinationVC = segue.destination as! Level3ViewController

            destinationVC.cScore = self.cScore
            destinationVC.aScore = self.aScore

        }

        //your scoring logic
        @IBAction func buttonCorrect(_ sender: UIButton)
         {
         cScore +=1
         aScore +=1

         correctScore.text = NSString(format: "%i", cScore) as String
         attemptScore.text = NSString(format: "%i", aScore) as String
         }

        @IBAction func buttonIncorrect(_ sender: UIButton)
        {
        cScore +=0
        aScore +=1
        correctScore.text = NSString(format: "%i", cScore) as String
        attemptScore.text = NSString(format: "%i", aScore) as String
        }

   }

You could then pass the final aScore and cScore variables to the final view controller using the same segue technique and display your chart.

NOTE: If you wish to persist the data, you could use a local database like Realm or Core Data to save the aScore and cScore. You could then pass a primary id of the saved object as an Int between the view controllers in the prepareForSegue method instead of aScore or cScore and retrieve the scores in the next view controller using the primary id.

gwinyai
  • 2,410
  • 2
  • 13
  • 16
  • This works great but everytime i got to the next viewcontroller the aScore and cScore is preset to 0 and then when i click a button they update? Is there a way of getting them to equal the value from the previous viewController? – Keiron Morris Mar 21 '17 at 13:22
  • Hi. I have edited the solution and changed the Level 2 controller's cScore and aScore to var cScore: Int! and var aScore: Int! to prevent them defaulting to 0. Let me know if this works. – gwinyai Mar 22 '17 at 09:25
  • Hi @gwyinyai in the second view controller where you have change the following code from: var aScore = 0 to var aScore: Int! it has come up with the following error message where it says: aScore += 1 Binary operator '+=' cannot be applied to operands of type 'Int!' and 'Int' – Keiron Morris Mar 22 '17 at 20:35
  • All sorted just put the following code in func viewDidLoad() correctScore.text = NSString(format: "%i", cScore) as String attemptScore.text = NSString(format: "%i", aScore) as String – Keiron Morris Mar 23 '17 at 06:40
0

You have a few options.

If you don't want to persist the data, and just pass it between the two view controllers, see Passing Data Between Controllers in Swift.

You could also persist the values between the two controllers (save it, then read it from the other controller) by using UserDefaults.

Community
  • 1
  • 1
Stephen
  • 172
  • 2
  • 7