由于SIGABRT,应用程式当机

时间:2018-08-29 00:21:39

标签: ios swift segue

我100%确信一切都已正确连接,但我仍然收到SIGABRT错误。

这是SelectViewController的代码:

import UIKit

class SelectViewController: UIViewController {

var accounttype = 0

@IBAction func AdminButton(_ sender: Any) {
}

@IBAction func ParentButton(_ sender: Any) {
}

@IBAction func DriverButton(_ sender: Any) {
}

@IBAction func StudentButton(_ sender: Any) {
    accounttype = 1
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    var SubmitController = segue.destination as! SubmitViewController
    SubmitController.john = accounttype;

}


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// 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.
}
*/

}

这是SubmitViewController的代码:

import UIKit

class SelectViewController: UIViewController {

var accountType = 0

@IBAction func AdminButton(_ sender: Any) {
}

@IBAction func ParentButton(_ sender: Any) {
}

@IBAction func DriverButton(_ sender: Any) {
}

@IBAction func StudentButton(_ sender: Any) {
    accountType = 1
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destination = segue.destination as? SubmitViewController {
        destination.john = accountType
        print("Inside if statement")
    }

}


override func viewDidLoad() {
    super.viewDidLoad()

     print("Out of if statement")
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// 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.
}
*/

}

这是控制台:

2018-08-28 17:17:56.529276-0700 SchoolDrive[5178:1005170] 5.4.1 - [Firebase/Analytics][I-ACS023007] Firebase Analytics v.50001000 started
2018-08-28 17:17:56.529921-0700 SchoolDrive[5178:1005170] 5.4.1 - [Firebase/Analytics][I-ACS023008] To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see https://help.apple.com/xcode/mac/8.0/#/dev3ec8a1cb4)
2018-08-28 17:17:57.024884-0700 SchoolDrive[5178:1005198] TIC Read Status [1:0x0]: 1:57
2018-08-28 17:17:57.025067-0700 SchoolDrive[5178:1005198] TIC Read Status [1:0x0]: 1:57
2018-08-28 17:18:01.737912-0700 SchoolDrive[5178:1005175] TIC Read Status [2:0x0]: 1:57
2018-08-28 17:18:01.738363-0700 SchoolDrive[5178:1005175] TIC Read Status [2:0x0]: 1:57
2018-08-28 17:18:03.755460-0700 SchoolDrive[5178:1005196] TIC Read Status [3:0x0]: 1:57
2018-08-28 17:18:03.755664-0700 SchoolDrive[5178:1005196] TIC Read Status [3:0x0]: 1:57
Could not cast value of type 'SchoolDrive.SecondRegisterViewController' (0x10d8349d0) to 'SchoolDrive.SubmitViewController' (0x10d834820).
2018-08-28 17:18:04.613651-0700 SchoolDrive[5178:1004484] Could not cast value of type 'SchoolDrive.SecondRegisterViewController' (0x10d8349d0) to 'SchoolDrive.SubmitViewController' (0x10d834820).
(lldb)

我非常感谢所有帮助,感谢所有帮助。先感谢您。我需要尽快获得所有帮助。这将使我更轻松。再次非常感谢您的帮助,我非常感谢。

3 个答案:

答案 0 :(得分:1)

由于要使用if letguard来防止崩溃而进行修复的力量使您崩溃。在下面的prepare(for:, sender:)方法中修改您的代码。

if let destination = segue.destination as? SubmitViewController {
  destination.john = accountType
}

guard let destination = segue.destination as? SubmitViewController else {
  return 
}

destination.john = accountType

答案 1 :(得分:0)

您需要先测试segue.destination,以确保它是SubmitViewController,然后再使用as!对其进行强制转换。如果使用SecondRegisterViewController,则堆栈跟踪将指示该实例的类型。

答案 2 :(得分:0)

根据您的错误提示,您可能将目标序列绑定到错误的VC(SecondRegisterViewController)。预期是SubmitViewController。 为了更安全,请使用以下方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "segueApplicationToSubmitControllerVC"
        {
            if let SubmitControllerVC = segue.destination as? SubmitViewController
            {
                SubmitControllerVC.john = accounttype

            }
        }
    }