Saving View Controller state

Discussion in 'iOS Programming' started by RossOliver, Aug 13, 2008.

  1. RossOliver macrumors regular

    Joined:
    Nov 6, 2006
    #1
    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
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    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.
     
  3. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #3
    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
     
  4. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #4
    Anyone? I can't seem to find any documentation about saving view controller state anywhere...

    -Ross
     
  5. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    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.
     
  6. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #6
    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
     
  7. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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...
     
  8. RossOliver thread starter macrumors regular

    Joined:
    Nov 6, 2006
    #8
    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!
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    I'll have a look at that before I implement the rest of my save/restore too then :)
     

Share This Page