【问题标题】:Saving Data from a Label on multiple View Controllers在多个视图控制器上保存标签中的数据
【发布时间】:2017-03-05 18:34:53
【问题描述】:

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

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

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

【问题讨论】:

    标签: swift xcode


    【解决方案1】:

    假设您使用 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 级视图控制器的 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 在下一个视图控制器中检索分数。

    【讨论】:

    • 这很好用,但每次我进入下一个视图控制器时,aScore 和 cScore 都预设为 0,然后当我单击按钮时它们会更新?有没有办法让它们等于前一个 viewController 的值?
    • 嗨。我已经编辑了解决方案并将 2 级控制器的 cScore 和 aScore 更改为 var cScore: Int!和 var aScore: Int!以防止它们默认为 0。让我知道这是否有效。
    • 嗨@gwyinyai 在第二个视图控制器中,您将以下代码从:var aScore = 0 更改为 var aScore: Int!它提出了以下错误消息:aScore += 1 Binary operator '+=' cannot be applied to operands of type 'Int!'和“诠释”
    • 所有排序只需将以下代码放入 func viewDidLoad() correctScore.text = NSString(format: "%i", cScore) as String attemptScore.text = NSString(format: "%i", aScore) 作为字符串
    【解决方案2】:

    你有几个选择。

    如果您不想持久化数据,而只是在两个视图控制器之间传递,请参阅Passing Data Between Controllers in Swift

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 2012-02-02
      • 2015-02-10
      • 2021-06-03
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多