0

I want to initData for two view controllers that I don't want to present immediately.

Atm I have when a user taps a tableViewCell (select a group) they are presented with "chatVC" and the appropriate data is presented. In ChatVC the user can access two other VC, ChatInfoVC and ChatInfoSettingsVC. Is there a way to pass the right info from the tableViewCell (the group as I do when I present the ChatVC) to both the other VC? I'm pulling all data from Firebase.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   guard let chatVC = storyboard?.instantiateViewController(withIdentifier: "chat") as? ChatVC else { return }
    chatVC.initData(forGroup: groupsArray[indexPath.row])
    presentDetail(chatVC)
}

I appreciate all help.

rmaddy
  • 307,833
  • 40
  • 508
  • 550

3 Answers3

0

Option

A: store your VC in class property, assign it before presenting it in the didselectRow.

B: create a class property to store the indexpath of the selected one. Your VC data is inside groupsArray, u just need the indexpath to get it. Then get data, init controller, assign that data to it.

alegelos
  • 1,948
  • 13
  • 21
0

Typically your ChatVC will provide the data to the viewcontrollers it presents. Therefore, you'd have to either

  • hand that data from the current viewcontroller to ChatVC and from there further to the next two viewcontrollers, or
  • fetch the detail data in ChatVC before presenting the two other viewcontrollers

You could also intantiiate those two viewcontrollers in tableView(_:didSelectRow:), provision them with the correct data, and then store those viewcontrollers in two properties of your ChatVC -- but this seem to me somehow strange

Andreas Oetjen
  • 9,151
  • 1
  • 21
  • 31
0

If I m not misunderstand you, you can just store the data to the ChatVc(declare a store property in ChatVc), when you ready to present the other two ViewControllers you can easily access the data.

Meonardo
  • 182
  • 13