繁体   English   中英

如何将高分保存到标签中?

[英]How to save the high score into the label?

由于某些原因,如果我的分数大于highScore,则它不执行我的功能。 任何帮助深表感谢。

这是我的代码:

import UIKit
import AVFoundation
struct Question {
    var Question : String!
    var Answers : [String]!
    var Answer : Int!

}

class ViewController: UIViewController {

    @IBOutlet weak var highScoreLbl: UILabel!
    @IBOutlet var Buttons: [UIButton]!
    @IBOutlet weak var QLabel: UILabel!
    @IBOutlet weak var Label: UILabel!
    @IBOutlet weak var incorrectLabel: UILabel!
    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var theEnd: UILabel!
    @IBOutlet weak var continueButton: UIButton!

    var scoreLbl = UILabel()

    var score = Int()

    var Questions = [Question]()

    var QNumber = Int()

    var AnswerNumber = Int()

    var wrongAnswers = Int()

    var highScore = 0

    override func viewDidLoad() {
        super.viewDidLoad()


        Questions = [Question(Question: "What is the Biggest Hit of Bing Crosby?" , Answers: ["Swinging on a Star", "Now is the Hour", "White Christmas", "Beautiful Dreamer"], Answer: 2),
        Question(Question: "What is Elvis Presely's Middle Name?", Answers: ["Aaron", "Micheal", "George", "Matthew"], Answer: 0),
        Question(Question: "How Many Oscars did Titanic win?", Answers: ["5", "7", "10", "11"], Answer: 3),
        Question(Question: "From which country did Pitta Bread originate?", Answers: ["Spain", "France", "Greece", "Russia"], Answer: 2),
        Question(Question: "What is the largest living creature on Earth?", Answers: ["Whale", "Shark", "Sea Turtle", "Alligator"], Answer: 0),
        Question(Question: "What does ATM stand for?", Answers: ["Automatic Treasure Machine", "Automatic Tax Machine", "Anti Tax Machine", "Automatic Teller Machine"], Answer: 3),
        Question(Question: "What's the world's second largest French speaking city?", Answers: ["Paris", "Montreal", "Versailles", "Québec"], Answer: 1),
        Question(Question: "What Country is the largest producer of Olive Oil?", Answers: ["Italy", "France", "Greece", "Spain"], Answer: 3),
        Question(Question: "How long is the Great Wall of China?", Answers: ["3200 miles", "4000 miles", "2000 kilometers", "4500 miles"], Answer: 1),
        Question(Question: "Who is on the 10 dollar bill?", Answers: ["George Washington", "Thomas Jefferson", "Alexander Hamilton", "John Adams" ], Answer: 2),
        Question(Question: "How many World Series did Yogi Berra win as a player?", Answers: ["11", "10", "5", "7" ], Answer: 1),
        Question(Question: "Which three countries hosted the Winter Olympics during the 1990's?", Answers: ["Norway, France, Russia", "US, Sweeden, Canada",

            "Japan, Canada, Germany", "Slovenia, France, South Korea" ], Answer: 0),]



        scoreLbl = UILabel(frame: CGRectMake(35, 45, 77, 45))
        scoreLbl.textAlignment = NSTextAlignment.Center
        scoreLbl.text = "-1"
        self.view.addSubview(scoreLbl)

        PickQuestions()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func PickQuestions(){

        score++
        scoreLbl.text = "\(score)"

        if Questions.count > 0{
            QNumber = random() % Questions.count
            QLabel.text = Questions[QNumber].Question

            AnswerNumber = Questions[QNumber].Answer

            for i in 0..<Buttons.count{
                Buttons[i].setTitle(Questions[QNumber].Answers[i], forState: UIControlState.Normal)
            }

            Questions.removeAtIndex(QNumber)
        }
        else{

            theEnd.text = "You Win!"
            theEnd.alpha = 1
        }



        incorrectLabel.alpha = 0

    }

    func saveHighScore(){

        if score >= highScore {
            highScore == score
            highScoreLbl.text = "High Score: " + String(highScore)

        }
        else{}

    }


    @IBAction func Btn1(sender: AnyObject) {
        if AnswerNumber == 0{
            PickQuestions()
        }
        else{
            incorrectLabel.text = "You are incorrect!"
            incorrectLabel.alpha = 1
            score--
            scoreLbl.text = "\(score)"
        }

    }
    @IBAction func Btn2(sender: AnyObject) {
        if AnswerNumber == 1{

            PickQuestions()
        }
        else{
            incorrectLabel.text = "You are Incorrect!"
            incorrectLabel.alpha = 1
            score--
            scoreLbl.text = "\(score)"
        }
    }
    @IBAction func Btn3(sender: AnyObject) {
        if AnswerNumber == 2{

            PickQuestions()
        }
        else{
            incorrectLabel.text = "You are Incorrect!"
            incorrectLabel.alpha = 1
            score--
            scoreLbl.text = "\(score)"
        }
    }
    @IBAction func Btn4(sender: AnyObject) {
        if AnswerNumber == 3{

            PickQuestions()
        }
        else{
            incorrectLabel.text = "You are Incorrect!"
            incorrectLabel.alpha = 1
            score--
            scoreLbl.text = "\(score)"
        }
    }
}

highScore == score应为highScore = score ,否则highScore始终为0

看起来saveHighScore永远也不会被调用

 func PickQuestions(){

    score++
    saveHighScore()   <--- need to do things like this when score is updated
    scoreLbl.text = "\(score)"

保存:

//How to save result, do this after the highscore is set 
let defaults = NSUserDefaults.standardUserDefaults()
defaults["highScore"] = highScore

//How to read saved result, do this in your viewdidload
let defaults = NSUserDefaults.standardUserDefaults()
if let hs = defaults["highScore"] 
{
    highScore = hs
}

现在您可能必须强制转换为正确的类型才能使其正常工作,但是上面概述了基本概念

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM