Я получаю эту ошибку, когда пытаюсь выполнить переход к другому контроллеру представления. Я не знаю, почему я получаю эту ошибку?
Тема 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)
}
}
Вы не можете вызвать performSegue
в prepare(for segue
, это всегда будет приводить к ошибке, так как это будет вызываться рекурсивно
Убрать все
performSegue(withIdentifier: "NormalPushupSegue", sender: self)
Прежде всего, лучше безопасно развернуть ваши контроллеры представления. Вот так:
if let myViewController = segue.destination as? MyViewController {
// Set up the VC, add some values and options
}
Во-вторых, вам не нужно вызывать выполнитьSegue , выполнение для перехода уже вызывает ваш контроллер представления. Просто удалите выполнитьSegue
Третье — можно упростить и применить такую логику:
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
}
}
}
}
О, я вижу, ты прав. Переход, который я уже подключил, выполняет переход, поэтому PerformSegue просто разбился, когда я нажал кнопку. Спасибо за дополнительную логику, я попробую.
Почему вы вызываете PerformSegue внутри prepareForSegue? Это приводит к бесконечным рекурсивным вызовам. Вызов PerformSegue вызовет метод prepareForSegue.