Saving the ViewController in the same state / saving it in the background?

I am currently working in an application with several views, 30+ ViewControllers. I use a storyboard to connect a stream of views together.

I would like that when the user goes from one ViewController, when they return to this view, he remains in the same state with the same changes that the user has entered. (in UITextFeild, UILabel, UITextView, etc.)

I, since I have so many views, it would be ideal not to store information in global variables, either with:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

Thanks in advance, Relations

+4
source share
2 answers

There are many ways to store data; global variables and the default system are usually not among the best options. It looks like you should consider developing a real data model for your application. A model is M in MVC, and it is essentially one or more objects that store data and manage the "business logic" for your application.

Your model can be as simple as one mutable dictionary, or it can be a complex graph of interconnected objects. I will not try to explain all of MVC here - you can (should) read about it if you are not familiar with the idea. But, of course, it is possible to create a model that saves the state of each view controller, and when this controller becomes active, it can adjust its hierarchy of views in accordance with the saved state.

+2
source

Basically you need to implement something like a stack, and when you call another viewController, you push it onto the stack, saving all the state the viewController was in and popping out of the stack, the last clicked viewController (& its state) will be displayed.

Fortunately, there is something similar in iOS - the UINavigationViewController . It provides methods such as -

 pushViewController:animated: – popViewControllerAnimated: – popToRootViewControllerAnimated: – popToViewController:animated: 

This guide describes how to implement this in your project. Therefore, instead of linking viewControllers via storyBoard, I suggest you roll up your sleeve and implement all this in code. Hope this helps.

+1
source

Source: https://habr.com/ru/post/1401093/


All Articles