Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
Hey,

I'm trying to save state in my application. So far I can successfully save the 'backend' of what the user has changed but not committed to the database via NSCoding and storing the resulting NSData object as a User Default.

The problem I am having is in restoring the UIViewControllers. My setup consists of a main view controller (loaded when the application starts) and then it can display one of two modal view controllers:

...........................MyAppDelegate..............................
.........................MainViewController...........................
....ModalViewController1.....ModalViewController2....

I originally tried to make the MainViewController/ModalViewController1/2 NSCoding compliant, so when my app terminates it archives MainViewController, which will then go on to archive its modal view controller (and they archive the backed instance). I was then going to restore the MainViewController when my application restarts, presenting the correct modal view controller within its initWithCoder: method, but it didn't really go to plan. I got errors from encoding view controllers that contained UIImage's (I wasn't trying to encode the UIImage directly since it doesn't support NSCoding, but when I call [super encodeWithCoder:coder] the UIViewController seems to try and encode them).

Am I going about this the right way and just messing something up or is there a different, more correct approach?

Thanks for your time,

-Ross
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
I wouldn't use NSCoding for your controllers. Use NSUserDefaults for specific values that need to be saved, or use NSCoding on your model objects.
 

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
I wouldn't use NSCoding for your controllers. Use NSUserDefaults for specific values that need to be saved, or use NSCoding on your model objects.

So how do you go about saving which view controller the user had selected? I guess you could build a tree of class names from the AppDelegate object to the foremost view controller when the application terminates, but that seems a bit of a workaround - surely there is a better way?

Cheers
 

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
Anyone? I can't seem to find any documentation about saving view controller state anywhere...

-Ross
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
At this stage I am only saving which tab the user has selected in my main tab view. I just save the selected tab index to a pre-determined user defaults key and restore that. For the actual views themselves I am envisaging asking each for an NSDictionary of key/value pairs to store and the providing them the same dictionary on re-start to allow them to save and load state.
 

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
At this stage I am only saving which tab the user has selected in my main tab view. I just save the selected tab index to a pre-determined user defaults key and restore that. For the actual views themselves I am envisaging asking each for an NSDictionary of key/value pairs to store and the providing them the same dictionary on re-start to allow them to save and load state.

So do you simply pass the view controller the NSDictionary of key/value pairs when it initializes? This wouldn't seem to scale well if you had several levels of views, all of them needing to pass their NSDictionary back to the main view - restoring all of them would be a nightmare.

It seems that would just be a workaround for not using NSCoding, which would automatically encode/decode the instance variables of a view controller, plus any other variables you wanted to encode in the NSCoding protocol methods (including higher level view controllers). I don't understand why encoding a UIViewController isn't working since it complies with the NSCoding protocol and should be as simple as:

[application closes]
AppDelegate - archive mainViewController
mainViewController - encode, encode modalViewController
modalViewController - encode, encode backend

[application re-opens]
AppDelegate - unarchive, set mainViewController
mainViewController - decode, decode and present modalViewController
modalViewController - decode, decode backend

That seems perfectly logical to me (although my explanation may be hard to interpret)...

-Ross
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
So do you simply pass the view controller the NSDictionary of key/value pairs when it initializes? This wouldn't seem to scale well if you had several levels of views, all of them needing to pass their NSDictionary back to the main view - restoring all of them would be a nightmare.

Basically yes. I don't have a very deep UI and only really need to hold 2 levels of views. I'm not trying to solve the general problem...

I think the problem with using NSCoding to archive the entire view structure is that you are likely to be saving a lot more data that you need. I only envisage save 3 or 4 variables per view...

Edit to add: the approach I am taking is basically the same as when windows auto-save their state on OSX (Desktop). The entire window does not get encoded and saved, rather some key variables defining it's position and size get saved to the defaults...
 

RossOliver

macrumors regular
Original poster
Nov 6, 2006
157
0
Basically yes. I don't have a very deep UI and only really need to hold 2 levels of views. I'm not trying to solve the general problem...

I think the problem with using NSCoding to archive the entire view structure is that you are likely to be saving a lot more data that you need. I only envisage save 3 or 4 variables per view...

Edit to add: the approach I am taking is basically the same as when windows auto-save their state on OSX (Desktop). The entire window does not get encoded and saved, rather some key variables defining it's position and size get saved to the defaults...

Yea that does make sense, it will take up a bit of memory saving all the views - but I would have thought Apple would have provided a correct and standardized way of doing this... I wonder if they save view state in any of their sample applications...

-Ross

[edit]

I just looked and came across the godsend "DrillDownSave" - thank you Apple!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.