I ran into a problem when using tabBarController. My application has a tabBar and two tabs: Library and Playlists. The Library tab has a separate ViewController and the Playlists tab has a separate ViewController too. However, when I go to Playlists and select an any playlist, I create a new ViewController based on the LibraryViewController for my playlist and push it. At some point, my tabs in the tabBar are both LibraryViewControllers.
I also have a button with which I can return to the playlist I need from anywhere (I will not explain why I need it), I implement this using tabBar.selectedIndex and then push the playlist I need. But after clicking on this button, when I return to my Library tab, it becomes exactly the same as the Playlist tab (into which I have already pushed the ViewController with the playlist I need). I cannot understand why this is happening. I'm creating a new instance of the Library ViewController, why is my Library tab intercepting it?
(Sorry if this looks confusing, I tried to simplify, my application is actually more complicated.)
My code:
class MainTabBarController: UITabBarController {
...
override func viewDidLoad() {
super.viewDidLoad()
viewControllers = [generateVC(...), generateVC(...)]
}
private func generateVC(rootViewController: UIViewController, image: UIImage?, title: String) -> UIViewController {
let navigationVC = UINavigationController(rootViewController: rootViewController)
navigationVC.tabBarItem.image = image
navigationVC.tabBarItem.title = title
rootViewController.navigationItem.title = title
return navigationVC
}
}
class LibraryViewController:...{
...
func openCurrentTrackList() { //protocol function because button is in the some View
let tabBar = ...
tabBar.selectIndex = 1
let storyboard = UIStoryboard(name: "LibraryViewController", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "Library") as! LibraryViewController
//Setup vc
...
navigationController?.pushViewController(vc, animated: false)
}
}
class PlaylistsViewController:...{
...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "LibraryViewController", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "Library") as! LibraryViewController
//Setup vc
...
navigationController?.pushViewController(vc, animated: false)
}
}