Micheal
Micheal

Reputation: 305

prepare for segue issue with multiple ViewControllers Not 100% working

There are 4 view controllers: raceViewController raceInfoViewController abilityCalcViewController abilityFinalViewController

Quick overview how the app works. First the user starts in abilityCalcViewController they update their stats then when that is done they click the Select Races Button which then goes to raceViewController. Here there are 2 buttons 1 leading to raceInfoViewController and the other to abilityFinalViewController. Each different will have its own set of information to pass between the ViewControllers.

The code that I have is:

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]
            }
        }
    }

My issue is when I click on the button that leads to abilityFinalViewController or the button going to raceInfoViewController it transitions but then none of the data is passed. So destFinalViewController.strStatScore = strStatScore seems to be blank when it gets to the the next destinations. What am I missing?

Upvotes: 0

Views: 61

Answers (1)

Duncan C
Duncan C

Reputation: 131511

Note that prepareForSegue code that decides what to do based on segue identifier is fragile. If you forget to set an identifier, or have a typo in your identifier, it doesn't work.

In Swift, I prefer to use a switch statement that switches based on the class of the destination view controller:

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
    }
}

That case let syntax does double duty, since it gives you local variable cast to the correct type based on the class of the destination view controller.

By the way, class names and type names should always start with an upper-case letter in Swift. Variable names should start with a lower case letter. Thus your abilityFinalViewController class should be named AbilityFinalViewController and raceInfoViewController should be RaceInfoViewController.

Upvotes: 1

Related Questions