views:

113

answers:

2

(By the way, I don't use Interface Builder)

I have a little project consisting of a root UIViewController which manages 5 other View Controllers with a UITabBar, I'm not using UITabBarController. The Root View Controller only ever keeps one of its View Controllers instantiated, when a tab bar item is selected, the View Controller associated with that tab gets instantiated, the old one is removed, the new one is added as a subview and the old one is deallocated.

Each View controller has a UIPickerView (except one that has a UIDatePicker). When the user goes from tab 1 to tab 2 and the back to tab 1, I want the View Controller to be instantiated with the same selected row in the picker as it was before, even though it got deallocated, so I understand its best to use instances of a Model class to store information about a View Controller's state and then make init methods in each View Controller that initialize with an argument given which is an instance of the Model.

So could someone help me out? What should the Model class look like? What should it be named? How can an instance of it be used to store information about the state of one View Controller and another instance store information about another, different View Controller? I really want to get this simple project as perfect as it can be and to follow the MVC design pattern, so I can base all my other tab bar applications off this.

Thanks!!

A: 

Take a look at NSMutableDictionary, specifically the section of writing and reading to a plist file. I believe Apple's got a sample app floating around somewhere which uses this approach.

Basically you write out your view hierarchy into a dictionary and store it when the app quits. When you relaunch you read in and configure your views again.

jbrennan
I'm talking about when the view deallocates not when the app quits.
Mk12
You can still use this approach, but I would recommend not doing it in `dealloc`. Do your saving in `-viewDidUnload` or somesuch method, and then restore the state in `-viewDidLoad` or `-viewWillAppear:`
jbrennan
I want to use a model class, not a property list file.
Mk12
viewDidUnload never gets called because I free the entire view controller from memory when it isn't being displayed.
Mk12
A: 

OK well to anyone else wondering about this, I made a class, called it Model, and made it a singleton with Download SynthesizeSingleton.h.zip macro. You can use KVO (Key-Value Observing) to do stuff when stuff in the Model changes, learn about that Here, and you can e.g. have a UITextField delegate method for when the Text Field is edited and in that method set the ivar in the Model for your text field with the new text the user changed it to.

Mk12