I want to make a simple game, players can adjust some value in this ViewController
. But when I touch save
button, this app always be closed. I don't know how to fix this problem.
Code:
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")
}
}
}
Error:
Action:
Based on the information that you've given us there are two things that you should check that might cause your crash:
By the way, I would not use the name destination for the segue destination view controller, but rather use destinationVC or a more explicit destinationQMGameVC.
I hope this resolves your problem. Otherwise I would need more information.
Kind regards, MacUserT
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.