我在一个应用程序中有多个视图控制器,它们代表游戏中的不同级别。
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?
目的是能够在游戏结束时保存数据,并使用它来绘制图表,以显示用户对该问题进行了多少次尝试以及他们正确得分的实际数量?
最佳答案
假设您将Storyboard用于布局,则我在下面编写了一个可能的解决方案。基本上,您保留aScore和cScore的运行总计,并使用prepareForSegue将运行的总计传递给下一个视图控制器的aScore和cScore变量。我创建了一个名为procedToNextLevel的函数,只要您想进入下一个级别,就可以调用该函数。
您的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级View Controller中给出的零值。我在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方法中(而不是aScore或cScore)在视图控制器之间将存储对象的主ID作为Int传递,并使用主ID在下一个视图控制器中检索分数。
关于swift - 从多个 View Controller 上的标签保存数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42612328/