Segue to Tabbed view controller crashes

I have a view controller that was originally a single view, but as my app has expanded, it has turned into a tabbed view. I have an array that stores information about horses that gets imported to the TabbedHorseViewer when a button with the horse's name is clicked. The app works fine when it is just one VC, but now it is 5 different VCs connected the code doesn't really know how to handle it.

in the model:
Code Block
var myHorses = [
    Horse(name: "Donnerhall", idNumber: 1, gender: "Stallion")


My Horses: This is the VC with a list of horses in the form of buttons

Code Block         
        for horse in myHorses {
            let button = UIButton()
            button.addTarget(self, action: #selector(self.lookupHorse(sender:)), for: .touchUpInside)
            button.setTitleColor(.white, for: UIControl.State.normal)
            button.setTitle(horse.name, for: UIControl.State.normal)
            button.backgroundColor =  colorLiteral(red: 0.005273803137, green: 0.4785152674, blue: 0.3960535526, alpha: 1)
            buttonsStack.addArrangedSubview(button)
         }
         buttonsStack.translatesAutoresizingMaskIntoConstraints = false
    }
  
    private var horseSelected: Horse?
    @objc func lookupHorse(sender: UIButton){
            let horseName = sender.title(for: .normal)
            if let theHorse = myHorses.first(where: {$0.name == horseName}) {
                    horseSelected = theHorse
                    self.performSegue(withIdentifier: "horseViewerSegue", sender: self)
            }
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            switch segue.identifier {
            case "horseViewerSegue":
                let horseVC = segue.destination as! HorseViewController
                    horseVC.horsePassed = horseSelected
            default:
                    print("Unknown segue: \(segue.identifier ?? "nil")")
            }
    }


HorseViewController - This is the VC where some of the horse data is shown (there are 4 other VCs controlled by this tabbed view with more info)
Code Block
import UIKit
class HorseViewController: UIViewController {
    @IBOutlet weak var horseNameOutlet: UILabel!
    @IBOutlet weak var horseGenderOutlet: UILabel!
 var horsePassed: Horse?
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func viewWillAppear(_ animated: Bool) {
     if let theHorse = horsePassed {
        horseNameOutlet.text = theHorse.name
        horseGenderOutlet.text = theHorse.gender
}
}
}


When I run the app and try to click on a horse on the list, I get a "Thread 1: signal SIGABRT" error on line 32. The horseViewerSegue is pointing to the tab view controller. The app doesn't crash when I point the segue to the item within the tabbed view controller (the original setup that the code was written for) but in that case the tab bar at the bottom disappears. I want to be able to navigate to the VC and use the tabs, and I want all five tabs to collect different data about the same horse that was clicked in the first VC, fetching data from the array.

Thanks in advance!

The horseViewerSegue is pointing to the tab view controller.

Then, segue.destination is a UITabBarController, not a HorseViewController.

You may need to get the UITabBarController first, and then get the HorseViewController from the UITabBarController.

Something like this:
Code Block
let kIndexHorseViewController = 0 //<-Change this to the actual index
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case "horseViewerSegue":
let tabBarVC = segue.destination as! UITabBarController
let horseVC = tabBarVC.viewControllers![kIndexHorseViewController] as! HorseViewController
horseVC.horsePassed = horseSelected
tabBarVC.selectedIndex = kIndexHorseViewController
default:
print("Unknown segue: \(segue.identifier ?? "nil")")
}
}


Please try.
Yeah, it makes more sense to ask it to search for a UITabBarController :) How do I find the index for kIndexHorseViewController? For now, I tested it with "1" as the index and it crashed with the same error at
Code Block
let horseVC = tabBarVC.viewControllers![kIndexHorseViewController] as! HorseViewController


How do I find the index for kIndexHorseViewController?

Depends on how you set up view controllers shown in the tab bar controller.

What is printed if you put print(tabBarVC.viewControllers)?
Do I type that into the debugger console? It gives me the following error when I do:
Code Block
error: unknown command shorthand suffix: '(tabBarVC.viewControllers)'

I have no swift file for the UITabBarController because "Most clients will not need to subclass UITabBarController."

Do I type that into the debugger console?

Sorry for not being clear. Please put the line print(tabBarVC.viewControllers) before the line let horseVC = ... of your swift file.

Most clients will not need to subclass UITabBarController.

That's true, Most clients. But if you want to do some special transition like you describe, subclassing may be a better option.
Thanks for the clarification. I get the following:
Code Block 2020-10-14 07:17:12.601737+0200 Tobiano[11289:1183798] [Storyboard] Unknown class PedigreeViewController in Interface Builder file.
2020-10-14 07:17:12.602493+0200 Tobiano[11289:1183798] [Storyboard] Unknown class HorseSettingsViewController in Interface Builder file.
[<UIViewController: 0x7f80c9c21f30>, <UIViewController: 0x7f80c9d0b080>, <Tobiano.FeedPlanViewController: 0x7f80cb023800>, <Tobiano.TrainingViewController: 0x7f80c9f0bbf0>, <Tobiano.HorseViewController: 0x7f80cb016c00>]
Could not cast value of type 'UIViewController' (0x7fff86e8caf8) to 'Tobiano.HorseViewController' (0x10eb13190).
2020-10-14 07:17:12.610662+0200 Tobiano[11289:1183798] Could not cast value of type 'UIViewController' (0x7fff86e8caf8) to 'Tobiano.HorseViewController' (0x10eb13190).
Could not cast value of type 'UIViewController' (0x7fff86e8caf8) to 'Tobiano.HorseViewController' (0x10eb13190).
CoreSimulator 732.17 - Device: iPhone 11 (A0DE279E-03DD-489A-BD23-29C452150AB1) - Runtime: iOS 14.0 (18A372) - DeviceType: iPhone 11


 I get the following:

Thanks. Seems you have put your HorseViewController as the last in the tab bar.
Code Block
[<UIViewController: 0x7f80c9c21f30>, //[0]
<UIViewController: 0x7f80c9d0b080>, //[1]
<Tobiano.FeedPlanViewController: 0x7f80cb023800>, //[2]
<Tobiano.TrainingViewController: 0x7f80c9f0bbf0>, //[3]
<Tobiano.HorseViewController: 0x7f80cb016c00>] //[4] <- `HorseViewControlle` here

Please try with let kIndexHorseViewController = 4.
(Of course you may need to change this number depending on the settings of the tab bar.)

Fantastic! I ran into some trouble but found this and now the tabs are all working! Now I need to see if I can reorder these tabs. Thanks very much for the help :)
Segue to Tabbed view controller crashes
 
 
Q