Very confused about something, I have my first view controller, which will allow a user to type in a word (including wildcards) and that view will return a tableview of results, which is just a list of matching words to the search. The user then selects that word and it navigates to a tabbar controller where the tabs will be things like definitions, synonyms etc. I have a custom object which I pass from the first view to the tabbar controller in didSelectRowAt but it's not working. In the tabbar controller, in viewWillAppear it has the object. But by the time viewDidLoad runs the object is nil. I don't currently have any code in viewDidAppear apart from a print statement so I don't know where it's going missing
FirstViewController
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var word: WordDetails?
word = results[indexPath.row]
let detailVC = TabBarController()
detailVC.selectedWord = word
detailVC.navigationItem.title = word?.word
tableView.deselectRow(at: indexPath, animated: true)
navigationController?.pushViewController(detailVC, animated: true)
}
TabBarController
class TabBarController: UITabBarController, UITabBarControllerDelegate {
var tabOne: UIViewController?
var tabTwo: UIViewController?
var selectedWord: WordDetails?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
tabOne = DefinitionViewController()
let tabOneImage = UIImage.init(systemName: "book")
let tabOneBarItem = UITabBarItem(title: "Definitions", image: tabOneImage, selectedImage: tabOneImage)
tabOne?.tabBarItem = tabOneBarItem
tabTwo = SynonymViewController()
let tabTwoImage = UIImage.init(systemName: "quote.bubble")
let tabTwoBarItem = UITabBarItem(title: "Synonyms", image: tabTwoImage, selectedImage: tabTwoImage)
tabTwo?.tabBarItem = tabTwoBarItem
self.viewControllers = [tabOne!,tabTwo!]
}}
Any guidance would be amazing, I'm sure it's me just misunderstanding how tabbar controllers work
UPDATE
So I almost definitely misunderstood the view lifecycle. The fact that I have the value in viewWillAppear is good. However, how do I pass that to any of the tab view controllers?
I tried to do something like this... tabOne.selectedWord = self.selectedWord but it errors with Value of type 'UIViewController' has no member 'selectedWord'