在多个View控制器上保存标签中的数据

时间:2017-03-05 18:34:53

标签: swift xcode

我在应用程序中有多个视图控制器,代表游戏中的不同级别..

ViewController.swift SecondViewController.Swift等

在每个不同的级别上有几个按钮,1是正确的答案,2是不正确的。在每个视图控制器上,我还有标签,用于更新按钮内用户的分数......

@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
}

如何保存此级别生成的数据/数字并将其传递给SecondViewController.swift?

目的是能够在游戏结束时保存数据,并使用它来绘制用户在问题上尝试的次数与他们所拥有的实际得分数量的图表?

2 个答案:

答案 0 :(得分:0)

假设您正在使用Storyboard进行布局,我在下面写了一个可能的解决方案。基本上,您保持aScore和cScore的总计,并使用prepareForSegue将运行总计传递给下一个视图控制器的aScore和cScore变量。我创建了一个名为proceedToNextLevel的函数,只要你想进入下一个级别就会调用它。

你的1级View Controller看起来像这样:

    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
        }


    }

您的第2级视图控制器的aScore和cScore将由Level1ViewController中的prepareForSegue方法初始化为您在级别1中的总数。如果它们未初始化,则默认为级别2中给出的零值查看控制器。我保留了在2级视图控制器中增加aScore和cScore相同的逻辑。

    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
        }

   }

然后,您可以使用相同的segue技术将最终的aScore和cScore变量传递给最终视图控制器,并显示您的图表。

注意:如果您希望保留数据,可以使用Realm或Core Data等本地数据库来保存aScore和cScore。然后,您可以在prepareForSegue方法中的视图控制器之间传递已保存对象的主ID作为Int,而不是aScore或cScore,并使用主ID在下一个视图控制器中检索分数。

答案 1 :(得分:0)

您有几个选择。

如果您不想保留数据,只需在两个视图控制器之间传递数据,请参阅Passing Data Between Controllers in Swift

您还可以使用UserDefaults在两个控制器之间保留值(保存,然后从另一个控制器读取)。