Search code examples
iosswiftuiviewcontrolleruinavigationcontroller

Refresh "root" data inside a UINavigationController and update data in navigation stack


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.


Solution

  • 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.