当我尝试对另一个视图控制器执行segue时出现此错误。我不知道为什么会收到此错误?
线程1:EXC_BAD_ACCESS
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "NormalPushupSegue" {
let normalVC = segue.destination as! PopupViewController
normalVC.formType = "Normal"
performSegue(withIdentifier: "NormalPushupSegue", sender: self)
}
if segue.identifier == "DiamondPushupSegue" {
let diamondVC = segue.destination as! PopupViewController
diamondVC.formType = "Diamond"
performSegue(withIdentifier: "DiamondPushupSegue", sender: self)
}
if segue.identifier == "WidePushupSegue" {
let wideVC = segue.destination as! PopupViewController
wideVC.formType = "Wide"
performSegue(withIdentifier: "WidePushupSegue", sender: self)
}
if segue.identifier == "DeclinePushupSegue" {
let declineVC = segue.destination as! PopupViewController
declineVC.formType = "Decline"
performSegue(withIdentifier: "DeclinePushupSegue", sender: self)
}
}
答案 0 :(得分:0)
您不能在performSegue
中调用prepare(for segue
,因为它将被递归调用,因此总是会导致错误
全部删除
performSegue(withIdentifier: "NormalPushupSegue", sender: self)
答案 1 :(得分:0)
首先最好安全地解开视图控制器。像这样:
if let myViewController = segue.destination as? MyViewController {
// Set up the VC, add some values and options
}
第二个-您不需要调用 performSegue ,执行segue已经在调用视图控制器。只需删除 performSegue
第三-您可以简化并应用这样的逻辑:
enum AppSegueName: String {
case Normal = "NormalPushupSegue"
case Diamond = "DiamondPushupSegue"
case Wide = "WidePushupSegue"
case Decline = "DeclinePushupSegue"
}
extension AppSegueName: CaseIterable {}
在准备函数中,使用switch \ case语句并将AppSegueName原始值与segue.identifier进行比较
像这样:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
AppSegueName.allCases.forEach {
if $0.rawValue == segue.identifier {
if let myViewController = segue.destination as? MyViewController {
myViewController.formType = $0 // formType is of type AppSegueName
}
}
}
}