gpt4 book ai didi

swift - 在多个 View Controller 上保存标签中的数据

转载 作者:行者123 更新时间:2023-11-30 12:38:07 24 4
gpt4 key购买 nike

我在应用程序中有多个 View Controller ,它们代表游戏中的不同级别。

ViewController.swiftSecondViewController.Swift 等

每个不同级别上都有几个按钮,1 是正确答案,2 是不正确答案。在每个 View Controller 上,我还有标签来更新按钮内用户的分数...

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

目的是能够在游戏结束时保存数据,并用它来绘制图表,显示用户对问题的尝试次数与他们获得的实际正确分数的数量?

最佳答案

假设您使用 Storyboard 进行布局,我在下面编写了一个可能的解决方案。基本上,您保留 aScore 和 cScore 的运行总计,并使用prepareForSegue 将运行总计传递到下一个 View Controller 的 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级 View Controller 的aScore和cScore将由Level1ViewController中的prepareForSegue方法初始化为您在1级中的总数。如果它们未初始化,则默认为2级 View Controller 中给定的零值。我在 2 级 View Controller 中保留了相同的增加 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 变量传递到最终的 View Controller 并显示您的图表。

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

关于swift - 在多个 View Controller 上保存标签中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42612328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com