gpt4 book ai didi

ios - 我正在尝试 Segue 并传递一个字符串,但出现错误

转载 作者:行者123 更新时间:2023-11-29 05:51:58 26 4
gpt4 key购买 nike

当我尝试执行到另一个 View Controller 的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)
}
}

最佳答案

首先,最好安全地解开 View Controller 。就像这样:

if let myViewController = segue.destination as? MyViewController { 
// Set up the VC, add some values and options
}

第二 - 您不需要调用 performSegue ,perform for segue 已经在调用您的 View Controller 。只需删除 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
}
}
}
}

关于ios - 我正在尝试 Segue 并传递一个字符串,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55579567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com