I am currently working in an app with multiple views, 30+ ViewControllers. I am using storyboard to connect the flow of the views together.
I would like it so that when the user navigates away from one ViewController, when they come back to that view it is left in the same state with the same changes that the user had entered. (into a UITextFeild, UILabel, UITextView etc.)
I since I have so many views it would be ideal to not have to store the info in Global Variables or with:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
Thanks in advance, Regards
There are plenty of ways to store data; global variables and the defaults system are not usually among the best choices. It sounds like you should think about developing a real data model for your application. A model is the M in MVC, and it's essentially one or more objects that store the data and manage the "business logic" for your application.
Your model might be as simple as a single mutable dictionary, or it might be a complex graph of inter-related objects. I won't try to explain all of MVC here -- you can (should) read about it if you're not familiar with the idea. But it's certainly possible to create a model that will save the state of each view controller, and when a given controller becomes active it can set up its view hierarchy to match the saved state.
Basically you need to implement something like a stack, while invoking another viewController you push it into the stack preserving whatever state the viewController was in & popping from the stack will bring the last pushed viewController (& its state) to view.
Luckily there is something like this already in iOS - UINavigationViewController. It provides such methods like -
– pushViewController:animated:
– popViewControllerAnimated:
– popToRootViewControllerAnimated:
– popToViewController:animated:
This tutorial will tell you how to implement this in your project. So Instead of linking viewControllers through storyBoard, i suggest you roll up your sleeve & implement all this in code. Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With