I have a UItabBarController (called tabBarController) composed of a number of options. I also have a UITableView, whose first row is an option that should make the user navigate to a specific viewController.
My didSelectRowAt delegate method looks something like this:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRowAt(at: indexPath)?.textLabel?.text == "Navigate to BrowseViewController" {
/* BrowseViewController is currently the second item in the
tabBarController, so I just select its index to navigate to it */
tabBarController?.selectedIndex = 2
}
}
Now, this works for my current situation because I know that the second item in tabBarController is the UIViewController that I am looking for, but I want to future-proof my app so that if the order of viewControllers in tabBarController is changed in the future, the tableView does not break.
In other words, I am wondering if there is a way to first extract the index of the viewController I am looking for from tabBarController, and then use that index to navigate to it, something like so:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRowAt(at: indexPath)?.textLabel?.text == "Navigate to BrowseViewController" {
let browseViewControllerIndex = Int()
/* iterate through tabBarController's VC's and if the type of the VC
is BrowseViewController, find its index and store it
in browseViewController */
}
}