-1

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'

jdez
  • 57
  • 1
  • 7
  • Change `var tabOne: UIViewController?` and `var tabTwo: UIViewController?` to `var tabOne: DefinitionViewController?` and `var tabTwo: SynonymViewController?` – matt Nov 02 '20 at 14:07
  • But even better: do _not_ pass anything to the child view controllers. After all, you've got the `selectedWord` right there in your tab bar controller; both children can see it from there. Why do they need another copy? – matt Nov 02 '20 at 14:09
  • Thanks Matt, makes perfect sense to use the variable from the parent, how do I call it? – jdez Nov 02 '20 at 14:16
  • It's the `parent`. Just cast to TabBarController. – matt Nov 02 '20 at 14:16

2 Answers2

0

I got your mistake.

var selectedWord is present in TabBarController class not in tabOne class.

Solution:

Make a var selectedWord in tabOne class. Then assign value from your viewWillAppear.

Hopefully this will work. If not then please share tabOne class code here.

Kudos
  • 1,224
  • 9
  • 21
  • I already have a selectedWord variable in tabOne (which is DefinitionViewController) but I can't reference it from the tabbarcontroller – jdez Nov 02 '20 at 14:17
  • @jdez I answered that problem in my first comment. – matt Nov 02 '20 at 14:19
0

Why do DefinitionViewController and SynonymViewController need another copy of the selectedWord? It is sitting right there in the TabBarController where they can see it. Simply have them refer to

(parent as? TabBarController).selectedWord
matt
  • 515,959
  • 87
  • 875
  • 1,141