简体   繁体   中英

iOS - Thread 1: Signal SIGABRT Error

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:

  1. Is your segue configured properly. Meaning, does your segue have the identifier "Name" set and did you drag the segue from your QMOptionViewController to your QMGameViewController? If not your app will try to resolve a nil property and crashes.
  2. Did you hook up the scene that is supposed to be managed by the QMGameViewController to this class in Storyboard. If you forgot to hook up the scene belonging to QMGameViewController and you explicitly cast the destination to this class, your app will crash, since the destination is not a QMGameViewController.

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.

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