如何在Swift中要求游戏的缩写

时间:2018-12-27 07:10:06

标签: swift label

我正在Swift中制作一个游戏,该游戏会记录高分和首字母缩写(类似于弹球记录),并将其作为标签显示在游戏屏幕上。我正在使用下面的代码在手机上记录高分和首字母缩写并进行更新。目前,该程序仅在我的手机上,因此我不关心在远程数据库上存储或更新数据。

我还不知道该怎么做(还)是弹出一个小窗口,要求用户在按下退出按钮时(如果他们当前得分很高)在键盘上输入姓名缩写。理想情况下,我希望它只能接受3个字符并在将用户带到其他视图控制器之前立即更新缩写标签。

@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var highScoreLabel: UILabel!
@IBOutlet weak var highScoreInitialsLabel: UILabel!
var score : Int = 0

//Stores and sets high score initials
var oldHighScoreInitials : String = "AAA"
var highScoreInitials : String {
    get {
        return UserDefaults.standard.string(forKey: "highScoreInitials") ?? "AAA"
    }
    set {
        UserDefaults.standard.set(newValue, forKey: "highScoreInitials")
    }
}


//Stores and sets high score
var oldHighScore : Int = 0
var highScore : Int {
    get {
        return UserDefaults.standard.integer(forKey: "highScore")
    }
    set {
        UserDefaults.standard.set(newValue, forKey: "highScore")
    }
}


override func viewDidLoad() {
    super.viewDidLoad()
    //Updates high score and initials labels with stored highest score and associated initials
    highScoreLabel.text = String(highScore)
    highScoreInitialsLabel.text = String(highScoreInitials)
    oldHighScore = highScore
    oldHighScoreInitials = highScoreInitials
}


//Asks for initials if new high score, and segues to Main VC
@IBAction func quitButtonPressed(_ sender: AnyObject) {
    if (score > highScore){
        highScore = score
        print("Ask for initials")
        print("Game over, thanks for playing!"
        print("Segue to Main VC")
    }

    else {
        print("Game over, thanks for playing!")
        print("Segue to Main VC")
    }
}

非常感谢任何提供帮助或建议的人。

2 个答案:

答案 0 :(得分:1)

在这里,使用UIAlertViewController非常简单:

//Asks for initials if new high score, and segues to Main VC
@IBAction func quitButtonPressed(_ sender: AnyObject) {
    if (score > highScore){
        highScore = score

        let alert = UIAlertController(title: "NEW HIGH SCORE", message: "Please enter your initials", preferredStyle: .alert)

        alert.addTextField(configurationHandler: configurationTextField)

        alert.addAction(UIAlertAction(title: "DONE", style: .default, handler:{ (action) in

        //First example of updating initials
        guard alert.textFields![0].text?.characters.count != 0 else{
            return
        }

        self.oldHighScore = self.highScore
        self.oldHighScoreInitials = alert.textFields![0].text

        //Segue to Main VC
        }))

        present(alert, animated: true, completion: nil)
    }

    else {
        print("Game over, thanks for playing!")
        print("Segue to Main VC")
    }
}

func configurationTextField(textField: UITextField!){
    textField.delegate = self
    textField.textAlignment = .center
    textField.placeholder = "_ _ _"
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard let text = textField.text else { return true }
    let count = text.count + string.count - range.length
    return count <= 3
}

//Second example of updating initials
func textFieldDidEndEditing(_ textField: UITextField) {
    oldHighScore = highScore
    oldHighScoreInitials = textField.text
}

最后,您的控制器应符合UITextFieldDelegate协议:

class yourController: UIViewController, UITextFieldDelegate

答案 1 :(得分:1)

在与Arie Pinto合作之后,我们确定我的segue阻止了警报的工作,并且出现了一个错误,表明我的程序正试图显示其视图不在窗口层次结构中的警报。

我通过ctrl +将退出按钮拖动到其他视图控制器来编程我的segue,而无需编写任何代码。

我的解决方案是在quitButtonPressed函数中编写performSegue代码,以允许警报和初始更新代码在更改窗口之前执行其操作。