R – iPhone dev – help with using Model

cocoa-touchiphonemodelmodel-view-controller

(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!!

Best Answer

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.