2017-03-05 78 views
0

我有一个应用程序内mulitplie视图控制器,其在游戏中表示不同层次..保存数据从多个视图控制器一个标签

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?

其目的是为了能够在游戏结束时保存数据,并用它来绘制用户在问题上有多少次尝试与他们拥有的正确分数的实际数量的关系图。

回答

0

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

你1级 - 视图 - 控制器会是这个样子:

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视图控制器的华擎和cScore将由prepareForSegue方法在Level1ViewController被初始化为你的等级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 
     } 

    } 

然后,您可以使用相同的技术赛格瑞通过最终华擎和cScore变量的最终视图控制器和显示在图表。

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

+0

这很好,但每次我到了下一个viewcontroller的aScore和cScore预设为0,然后当我点击一个按钮,他们更新?有没有办法让它们与前一个viewController的值相等? –

+0

嗨。我编辑了解决方案并将Level 2控制器的cScore和aScore更改为var cScore:Int!和var aScore:Int!以防止他们违约0.让我知道这是否有效。 – gwinyai

+0

Hi @gwyinyai在第二个视图控制器中,您将以下代码更改为: var aScore = 0 to var aScore:Int! 它提出了以下错误消息,它说: aScore + = 1 二元运算符'+ ='不能应用于'Int!'类型的操作数和'Int' –

相关问题