Date
1 - 3 of 3
Action on Pop
iOS 13+, Xcode 11.4.1, Storyboard-based application.
I am using a navigation stack to manage some data edits. That is, • nav-controller root is a list of items. • tap on item to segue to a detail panel to edit an item (which is a collection of subItems) • subItems may either be edited on the detail panel or • tap on a subItem to segue to a subDetail panel ... etc. (Typically, this stack is three layers deep, but I'm looking for a solution that works for arbitrary depth.) Now for all levels of this hierarchy, if the user taps the back button (or performs a gesture with same effect), I need to recognize this fact so that I can pass data up the hierarchy (hopefully without requiring strong coupling throughout). The way I'm currently doing this is to execute a closure in each panel's deinit method. (Note that viewWillDisappear does not work except in the case of stack-depth == 2)). However putting this kind of code in deinit seems to me to be a code-smell. Can you suggest an alternate that works at all levels of an arbitrarily deep navigation stack? Rick Aurbach |
|
Hey Rick,
toggle quoted message
Show quoted text
Pass a reference to the data model to the incoming view controller as it’s initialized for pushing onto the stack. The view controller can then make changes to the model as they happen, and doesn’t need to care about its own life cycle. Deinit is, as you acknowledge, very much the wrong way to do it, in part because its effect relies on assumptions about memory management (which is conceptually unrelated to the business of the view controller). -ben On Apr 28, 2020, at 3:58 PM, Rick Aurbach via groups.io <rlaurb@...> wrote:
|
|
Ben,
Thanks. You're right. I was getting hung up by thinking that I had to combine the updating of the model and the updating of the UI that displays model elements. But in fact, I can update the model continuously, as long as the UI is always updated in viewWillAppear(). Cheers, Rick |
|