0

I'm using storyboard with a UINavigationController that has view controllers A, B and C.

A is the root view controller of the navigation controller which at startup fetches some data. If I click on a row in A that will segue to B view controller with that data. Clicking on a row in B will segue to C view controller.

Later in my app for instance I'm inside C and the user wants to refresh the data. Meaning I need to fetch the root data for A view controller again.

How do I rebuild my navigation hierarchy again after the user have refreshed the data? I need to update the data source in A and then in B and then finally in C somehow.

matt
  • 515,959
  • 87
  • 875
  • 1,141
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193

2 Answers2

0

It's merely a matter of planning ahead, isn't it? It's your program so it's your job to provide yourself with the facilities that you will need. In this situation there is no need to "rebuild" anything (though you could do that, I suppose). A and B both do still exist even while you're showing C, so all you have to do is communicate with them as needed. If the data-fetching power resides only in A, then simply provide A with a method that C can call in order to request the refresh and receive the updated data. If A and B are subsequently shown again (because the user pops back from C), they will of course use their viewWillAppear to update their respective interfaces as well. But at the moment A and B are merely objects, and communicating between objects is, after all, what object-oriented programming is all about.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Well A "houses" the fetched data. B is showing a subset of A's data and C is showing a subset of B's data. I'm thinking that after I have done the fetch again I need to update the data property in A, B and C? Meaning I need to get a reference to the navigation controller and access it's view controller property to get access to the navigation stack and update the data is the view controllers right? – Peter Warbo Dec 09 '16 at 21:24
  • That is certainly one way, as I describe here: http://stackoverflow.com/a/40986685/341994 But there are other ways, perhaps better. For example, you could pass along a reference to A, from A to B and then from B to C. Or, going even further, you could pass along a reference _to the data_ from A to B and then from B to C, perhaps wrapped in a helper object that has the knowledge of how to do the refresh. That is how _I_ would do it. Apple calls this pattern "dependency injection": provide each successive view controller with all that it will need. – matt Dec 09 '16 at 21:49
0

You should put all the logic of A in another objet which could be accessible from A and C. I think this post is a good place to start

GaétanZ
  • 4,870
  • 1
  • 23
  • 30