准备多个ViewControllers的segue问题不是100%工作

时间:2016-12-21 16:28:47

标签: ios swift uiviewcontroller segue

有4个视图控制器: raceViewController raceInfoViewController abilityCalcViewController abilityFinalViewController

快速了解应用的工作原理。首先,用户在abilityCalcViewController中启动他们更新他们的统计数据然后当他们完成后,他们点击Select Races按钮然后转到raceViewController。这里有2个按钮1通向raceInfoViewController,另一个通向abilityFinalViewController。每个不同的都有自己的信息集在ViewControllers之间传递。

我的代码是:

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在到达下一个目的地时似乎是空白的。我错过了什么?

1 个答案:

答案 0 :(得分:1)

请注意,根据segue标识符决定执行操作的prepareForSegue代码是脆弱的。如果您忘记设置标识符,或者标识符中有拼写错误,则无效。

在Swift中,我更喜欢使用基于目标视图控制器类切换的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语法具有双重功能,因为它根据目标视图控制器的类为您提供了正确类型的局部变量。

顺便说一下,类名和类型名应该始终以Swift中的大写字母开头。变量名称应以小写字母开头。因此,您的abilityFinalViewController课程应该命名为AbilityFinalViewController,而raceInfoViewController应该是RaceInfoViewController