gpt4 book ai didi

ios - xcode swift : NSUserDefaults : save the highscore if quit app and restart it where left off

转载 作者:可可西里 更新时间:2023-11-01 01:05:38 25 4
gpt4 key购买 nike

我正在制作一个测验应用程序。所以基本上我有分数和高分,我想在主视图 Controller 上显示和存储结果。

我可以显示结果但不能保存它们。请帮忙!我还希望如果我在问题 2 上退出我的应用程序,它将从那里重新启动而不是 isInitialViewController。非常感谢对这两个主题的任何帮助。

下面是加载乐谱的主要配置文件的代码,但是一旦您退出应用程序并重新加载它,数字就会消失:

import UIKit
import Parse
import ParseUI
import CoreData
import Foundation

class UserProfileViewController: UIViewController {

@IBOutlet var scoreLabelUser: UILabel!
var scoringString = String()

@IBOutlet var HighScoreLabelUser: UILabel!
var highscoreString = String()

@IBOutlet var HighScoreLabel: UILabel!
var HighscoreInt = 0

override func viewDidLoad() {
super.viewDidLoad()

var highScoreDefault2 = NSUserDefaults.standardUserDefaults()
if (highScoreDefault2.valueForKey("EASYHIGHSCORE") != nil)
{
HighscoreInt = highScoreDefault2.valueForKey("EASYHIGHSCORE") as! NSInteger
//highscoreString = highScoreDefault2.valueForKey("EASYHIGHSCORE") as! String
HighScoreLabel.text = NSString(format: "%i", HighscoreInt) as String

}

assignScores()

}



func assignScores() {

scoreLabelUser.text = NSString(format: "%@", scoringString) as String
HighScoreLabelUser.text = NSString(format: "%@", highscoreString) as String
HighScoreLabel.text = "\(highscoreString)"

var highScoreDefault2 = NSUserDefaults.standardUserDefaults()
highScoreDefault2.setValue(HighscoreInt, forKey: "EASYHIGHSCORE")

}

}

我有第二个 ViewController,它实际上有测验并正在计算分数和高分。

//
// EasyQuizViewController.swift
// Quiz

import UIKit
import CoreData
import AVFoundation

class EasyQuizViewController: UIViewController {

@IBOutlet var sequence: UILabel!

@IBOutlet var questionLabel: UILabel!

@IBOutlet var Button1: UIButton!
@IBOutlet var Button2: UIButton!
@IBOutlet var Button3: UIButton!
@IBOutlet var Button4: UIButton!

@IBOutlet var scoreLabel: UILabel!
@IBOutlet var highscoreLabel: UILabel!

var CorrectAnswer = String()
var questionNumber = 0

var score = 0
var highScore = 0


override func viewDidLoad() {
super.viewDidLoad()

var highScoreDefault = NSUserDefaults.standardUserDefaults()

if (highScoreDefault.valueForKey("HIGHSCORE") != nil)
{
highScore = highScoreDefault.valueForKey("HIGHSCORE") as! NSInteger!
highscoreLabel.text = NSString(format: "%i", highScore) as String
}
generateNewNumber()

// Do any additional setup after loading the view.
}


func generateNewNumber() {

questionNumber += 1

switch(questionNumber) {

case 1:

sequence.text = "\(questionNumber)/50"
questionLabel.text = "Which is the country with the most people?"
Button1.setTitle("United States of America", forState: UIControlState.Normal)
Button2.setTitle("South Africa", forState: UIControlState.Normal)
Button3.setTitle("China", forState: UIControlState.Normal)
Button4.setTitle("India", forState: UIControlState.Normal)

CorrectAnswer = "3"

break

case 2:

sequence.text = "\(questionNumber)/50"
questionLabel.text = "Which is the planet closest to the Sun?"
Button1.setTitle("Venus", forState: UIControlState.Normal)
Button2.setTitle("Jupiter", forState: UIControlState.Normal)
Button3.setTitle("Earth", forState: UIControlState.Normal)
Button4.setTitle("Mercury", forState: UIControlState.Normal)

CorrectAnswer = "4"

break


default:

var alert = UIAlertView(title: "Finished", message: "Your quiz has been completed. Click Okay to view scores.", delegate: self, cancelButtonTitle: "Ok")

alert.show()

//println("\(score)")
//println("\(highScore)")

scoreLabel.text = NSString(format: "%i", score) as String

if (score > highScore) {
highScore = score

highscoreLabel.text = NSString(format: "%i", highScore) as String
}


var highScoreDefault = NSUserDefaults.standardUserDefaults()

highScoreDefault.setValue(highScore, forKey: "HIGHSCORE")






self.profileScreen()


break

}

}




@IBAction func Button1Action(sender: AnyObject) {

if (CorrectAnswer == "1")
{

score += 1
generateNewNumber()
}
else {

generateNewNumber()
}
}

@IBAction func Button2Action(sender: AnyObject) {

if (CorrectAnswer == "2")
{

score += 1
generateNewNumber()
}
else {

generateNewNumber()
}
}

@IBAction func Button3Action(sender: AnyObject) {

if (CorrectAnswer == "3")
{

score += 1
generateNewNumber()
}
else {

generateNewNumber()
}
}

@IBAction func Button4Action(sender: AnyObject) {

if (CorrectAnswer == "4")
{

score += 1
generateNewNumber()
}
else {

generateNewNumber()
}
}


@IBAction func BackBtn(sender: AnyObject) {
self.performSegueWithIdentifier("backToProfile", sender: self)
}

func profileScreen() {
self.performSegueWithIdentifier("backToProfile", sender: self)
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destinationViewController : UserProfileViewController = segue.destinationViewController as! UserProfileViewController


//println("\(scoreLabel)")
//println("\(highscoreLabel)")

destinationViewController.scoringString = NSString(format: "%i", score) as String
destinationViewController.highscoreString = NSString(format: "%i", highScore) as String
//destinationViewController.HighscoreInt = NSString(format: "%i", highScore) as String
//destinationViewController.HighscoreInt = (highScore) as NSInteger


}


}

最佳答案

当用户退出您的应用时,您应该存储您的 NSUserDefaults:

func applicationDidEnterBackground(application: UIApplication) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.synchronize()
}

关于ios - xcode swift : NSUserDefaults : save the highscore if quit app and restart it where left off,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31200640/

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