我想制作一个简单的游戏,玩家可以在这个ViewController
中调整一些值。但是当我触摸save
按钮时,此应用程序始终会关闭。我不知道如何解决这个问题。
代码:
class QMOptionViewController: UIViewController {
@IBOutlet weak var TimeLabel: UILabel!
@IBOutlet weak var ScoreLabel: UILabel!
var ScoretoWin = Int(10)
var Time = Int(60)
override func viewDidLoad() {
super.viewDidLoad()
TimeLabel.text = "\(Time)"
ScoreLabel.text = "\(ScoretoWin)"
// Do any additional setup after loading the view.
}
@IBAction func TimeStepper(_ sender: UIStepper) {
let stepperval = sender.value
Time = Int(stepperval)
TimeLabel.text = "\(stepperval)"
}
@IBAction func ScoreStepper(_ sender: UIStepper) {
let stepperval = sender.value
ScoretoWin = Int(stepperval)
ScoreLabel.text = "\(stepperval)"
}
@IBAction func SaveButton(_ sender: Any) {
self.performSegue(withIdentifier: "Save", sender: self)
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "Save" {
let destination = segue.destination as! QMGameViewController
destination.timeleft = Time
destination.scoretowin = ScoretoWin
UserDefaults.standard.set(time, forKey: "totaltime")
}
}
}
错误:
行动:
答案 0 :(得分:0)
根据您提供给我们的信息,您应检查可能导致崩溃的两件事:
顺便说一句,我不会使用segue目标视图控制器的名称destination,而是使用destinationVC或更明确的destinationQMGameVC。
我希望这能解决你的问题。否则我需要更多信息。
亲切的问候, MacUserT