我正在尝试为单词打扰器应用制作排行榜,因此我先保存数据,然后再选择到最终将成为排行榜的下一个视图控制器。我的所有插座均已连接,并且正确输入了segue标识符,所以我看不到为什么按下完成后应用程序崩溃
错误行出现在这里:类AppDelegate:UIResponder,UIApplicationDelegate {
var finalScore = Int()
var playerName = String()
var allMyStoredData = UserDefaults.standard
class secondVC: UIViewController {
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var nameTF: UITextField!
@IBOutlet weak var doneButton: UIButton!
var playerScore = 0
override func viewDidLoad() {
super.viewDidLoad()
scoreLabel.text = "Your score is: \(finalScore)"
loadData()
}
@IBAction func donePressed(_ sender: Any) {
saveData()
//this part won't execute
performSegue(withIdentifier: "toLeaderboard", sender: self)
}
func saveData () {
playerName = nameTF.text!
playerScore = finalScore
allMyStoredData.set(playerName, forKey: "saveTheName")
allMyStoredData.set(playerScore, forKey: "saveTheScore")
}
func loadData () {
if let loadPlayerName:String = UserDefaults.standard.value(forKey: "saveTheName") as? String {
playerName = loadPlayerName
}
if let loadTheScore:Int = UserDefaults.standard.value(forKey: "saveTheName") as? Int {
playerScore = loadTheScore
}
}
}
答案 0 :(得分:1)
更新:视图控制器中有一个插座,未连接或使用过“ toLeaderboard”,所以我删除了它,现在代码正常了