gpt4 book ai didi

ios - 为多个 ViewController 未 100% 工作的 segue 问题做准备

转载 作者:行者123 更新时间:2023-11-28 15:59:40 24 4
gpt4 key购买 nike

有 4 个 View Controller :raceViewControllerraceInfoViewController能力CalcViewController能力最终 View Controller

快速概览该应用的工作原理。首先,用户在 abilityCalcViewController 中启动他们更新他们的统计数据,然后在完成后单击选择比赛按钮,然后转到 raceViewController。这里有 2 个按钮,一个指向 raceInfoViewController,另一个指向 abilityFinalViewController。每个不同的将有自己的一组信息在 ViewController 之间传递。

我的代码是:

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "abilityFinalViewController"
{
let destFinalViewController: abilityFinalViewController = segue.destination as! abilityFinalViewController

destFinalViewController.strStatScore = strStatScore
}
else if segue.identifier == "raceInfoViewController"
{
let destViewController: raceInfoViewController = segue.destination as! raceInfoViewController
// Dwarf
if raceTable.indexPathForSelectedRow == [0,0]
{
print(raceTable.indexPathForSelectedRow ?? 0)
destViewController.raceName = races[0]
destViewController.raceStat = stats[0]
}

// Elf
else if raceTable.indexPathForSelectedRow == [0,1]
{
destViewController.raceName = races[1]
destViewController.raceStat = stats[1]
}

// Gnome
else if raceTable.indexPathForSelectedRow == [0,2]
{
destViewController.raceName = races[2]
destViewController.raceStat = stats[2]
}

// Half Elf
else if raceTable.indexPathForSelectedRow == [0,3]
{
destViewController.raceName = races[3]
destViewController.raceStat = stats[3]
}

// Half-Orc
else if raceTable.indexPathForSelectedRow == [0,4]
{
destViewController.raceName = races[4]
destViewController.raceStat = stats[4]
}

// Halfling
else if raceTable.indexPathForSelectedRow == [0,5]
{
destViewController.raceName = races[5]
destViewController.raceStat = stats[5]
}

// Human
else if raceTable.indexPathForSelectedRow == [0,6]
{
destViewController.raceName = races[6]
destViewController.raceStat = stats[6]
}

// Ifrit
else if raceTable.indexPathForSelectedRow == [0,7]
{
destViewController.raceName = races[7]
destViewController.raceStat = stats[7]
}

// Oread
else if raceTable.indexPathForSelectedRow == [0,8]
{
destViewController.raceName = races[8]
destViewController.raceStat = stats[8]
}

// Sylph
else if raceTable.indexPathForSelectedRow == [0,9]
{
destViewController.raceName = races[9]
destViewController.raceStat = stats[9]
}

// Tengu
else if raceTable.indexPathForSelectedRow == [0,10]
{
destViewController.raceName = races[10]
destViewController.raceStat = stats[10]
}

// Undine
else if raceTable.indexPathForSelectedRow == [0,11]
{
destViewController.raceName = races[11]
destViewController.raceStat = stats[11]
}
}
}

我的问题是,当我单击指向 abilityFinalViewController 的按钮或指向 raceInfoViewController 的按钮时,它会进行转换,但随后不会传递任何数据。所以 destFinalViewController.strStatScore = strStatScore 在到达下一个目的地时似乎是空白的。我错过了什么?

最佳答案

请注意,根据 segue 标识符决定要做什么的 prepareForSegue 代码是脆弱的。如果您忘记设置标识符,或者标识符中有拼写错误,它将不起作用。

在 Swift 中,我更喜欢使用基于目标 View Controller 的类进行切换的 switch 语句:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.destinationViewController {

case let miniCarContainerViewController as MiniCartConatinerViewController:
//Code to configure MiniCartConatinerViewController
print("Destination is class MiniCartConatinerViewController")

case let addOnsItemViewController as AddonsItemViewController:
//Code to configure AddonsItemViewController
print("Destination is class MiniCartConatinerViewController")

default:
print("Destination is unknown class!")
break
}
}

case let 语法有双重职责,因为它可以根据目标 View Controller 的类将局部变量转换为正确的类型。

顺便说一下,类名和类型名在 Swift 中应该总是以大写字母开头。变量名应以小写字母开头。因此,您的 abilityFinalViewController 类应命名为 AbilityFinalViewControllerraceInfoViewController 应命名为 RaceInfoViewController

关于ios - 为多个 ViewController 未 100% 工作的 segue 问题做准备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41267524/

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