gpt4 book ai didi

swift - segue 如何仅与某些指定的按钮配合使用

转载 作者:行者123 更新时间:2023-11-30 12:02:36 24 4
gpt4 key购买 nike

我正在使用 Xcode 做一个项目。我有四个按钮,其中两个用于在 View 之间传递数据,并且希望另外两个按钮仅跳转到另一个 View 而不传递任何数据。前两个表现很好,将数据传递到另一个 View 。但是当我单击其余两个时,应用程序崩溃并显示:

"2017-10-30 23:08:11.970500-0400 xxx[2557:65602] Could not cast value of type 'xxx.goalInfoViewController' (0x107f01580) to 'xxx.IOViewController' (0x107f019a0)."

这是代码:

@IBAction func monthlyIncomeAction(_ sender: Any) {
numberForSegue = 1
performSegue(withIdentifier: "outcomeSegue", sender: self)
}
@IBAction func MonthlyOutcomeAction(_ sender: Any) {
numberForSegue = 2
performSegue(withIdentifier: "incomeSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: (Any)?) {
let ioController = segue.destination as! IOViewController
ioController.segueIndex = numberForSegue
}

最佳答案

您应该确保只有两个在 View 之间传递数据的segue可以在prepare方法中运行代码。让我们尝试使用下面的代码来实现 prepare 方法。

override func prepare(for segue: UIStoryboardSegue, sender: (Any)?) {
if segue.identifier == "outcomeSegue" || segue.identifier == "incomeSegue" {
return;
}

let ioController = segue.destination as! IOViewController
ioController.segueIndex = numberForSegue
}

或者

override func prepare(for segue: UIStoryboardSegue, sender: (Any)?) {
if segue.destination is IOViewController {
let ioController = segue.destination as! IOViewController
ioController.segueIndex = numberForSegue
}
}

关于swift - segue 如何仅与某些指定的按钮配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47027367/

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