0

I'm working on an app that has about 5-6 different views with inputs and buttons within each view. The user does not necessarily proceed through the views in a set order. One of the views has quite a bit of custom user interaction that subsequently builds out a visual list of text fields and labels. I need the user to be able to leave that view and then return at a later point with the constructed visual in tact.

To avoid having a monolithic view controller, I have each view corresponding to its own view controller and storyboard. As a result, the only main way I can find to navigate between them is via segues. The problem is that, after I dismiss a segue, any interactions or view updates for that view controller are lost when I return to it.

Some of the views only have inputs like a couple text fields but the one view can have upwards of 100+ mixed inputs depending on user interaction. Rather than trying to pass that data around when dismissing and then eventually reload and reconstruct the view, I'm hoping to find a way to hide and show view controllers without losing the visual updates within their views.

Is this possible at all?

Let me know if I'm being too vague and I can try to provide more detail.

Thanks in advance!

brustj
  • 11
  • This is probably where the "model" part of MCV comes in. Rather then relying on the controller to remain unchanged once it's dismissed, use some kind of model to store and restore the state as needed – MadProgrammer Nov 27 '19 at 04:24
  • you need to use some design pattern and some architecture, of not, your code won't be reusable, now your idea could be like this, check out (https://stackoverflow.com/questions/48562017/change-views-using-segmented-control) – Andres Gomez Nov 27 '19 at 04:35

2 Answers2

1

You don't have to use segues to display view controllers from storyboards.

You can just load the storyboard, instantiate the view controller and then use it (push or present it or even set it as the key window's root view controller). It's no problem:

// Suppose you have a 'Signup.storyboard' that has an initial viewController defined:

let signupStoryboard = UIStoryboard(name: "Signup", bundle: nil)
if let signupViewController = signupStoryboard.instantiateInitialViewController() {
    present(signupViewController, animated: true, completion: nil)
}

If the view controller you want to load is not the initial one, make sure you define a storyboard identifier for it (in the storyboard, e.g. identifier="signup"). Then use:

let signupViewController = signupStoryboard.instantiateViewController(identifier: "signup")

The view controller will be loaded from the respective storyboard and you can use it just as any other view controller.

You can even keep strong references to the controllers and re-use them again after other view controllers were visible in between.

Lutz
  • 1,734
  • 9
  • 18
  • Thanks for the quick response! I did try this approach as well but, unless I was doing something wrong, it still reloaded the view controller fresh each time I returned, removing all progress from previous visits. Is that a correct assumption? – brustj Nov 27 '19 at 13:03
  • @brustj every time you call `instantiateInitialViewController()` or `instantiateViewController(identifier:)` a new one will be returned. That's true. But you can call that method only once and then save the view controller instance somewhere in your data model and then later re-use the very same instance. Note however, that this may not be the most memory efficient way to preserve UI state. – Lutz Nov 27 '19 at 13:08
  • OK yea that makes sense. Seems like a slightly less desirable approach so I'll keep it in my back pocket in case nothing else surfaces. Appreciate the help! – brustj Nov 27 '19 at 13:10
0

For each of the view controller you need to manage its data source and update in as user changes data in UI. Also you have to store the datasource somewhere i.e. in database, userdefault ,core data etc. And if user again pushes any view controller load the data from its datasource to see the last changes preserved. You may use some design and architecture pattern for it. Here is the link https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52

Hope this will help.

Gaurav Malik
  • 1,220
  • 1
  • 7
  • 3
  • I totally agree that in an ideal world, I would just use the standard MVC approach. For simple apps, this makes total sense but in my case, it gets a little complex given that the view controller needs to render and then manage a bunch of text fields and labels based on the current model. If this were say React, I would know exactly how to do this but I'm finding it hard to translate to OOP. It sounds like I just need to rethink my model and how to completely render the view and all it's custom elements just from reading in that model. Won't be easy but not sure I have other options. Thanks! – brustj Nov 27 '19 at 13:06