views:

220

answers:

2

Using NSUserDefaults to save/load a few small values... it's pretty straightforward.

But WHERE would I place my SAVE or LOAD code?

I want the defaults to LOAD only if/when a certain view is displayed. I want the defaults to SAVE, only when that view is exited/unloaded/hidden.

(I created a simple app using the "view-based template" and have my string values on the view, inside of UITextFields.)

A: 

How about the viewWillAppear and dealloc/viewDidDisappear methods of that view's UIViewController?

Dave DeLong
I was fearful that viewWillAppear would run my code "too soon". (The view wasn't in existance yet.) I was fearful that viewDidDisappear or viewDidUnload would run my code "too late".(The view was already gone.) Do I have to do anything special to get viewDidDisappear or viewDidUnload to run? (Other than creating them.)
Bonnie
`viewWillAppear` runs as your view is about to show on the screen (so it must already exist). Likewise, `viewDidDisappear` will run after your view has been taken off the screen. `viewDidUnload` only gets called when your app is running out of memory (so not always)
Dave DeLong
In both cases, your view will still exist.
Dave DeLong
Do I have to do anything special to get viewDidDisappear to run? (Other than creating it.)
Bonnie
@Kimberly are you using a `UIViewController` to manage the view? I highly recommend reading: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
Dave DeLong
So I assume I *DO* have to jump-through-hoops and read tons of docs just to get viewDidDisappear to run, right? Ugh. I had to read/do *NOTHING* special to get view-will-load to run.
Bonnie
When I click on your link... it just gives a generic "who should read this" page. When I click on ANY of the specific links on the right... it says "page not found"... and then just redirects me right back to "who should read this" page again and again. Thanks, Apple. Great docs. (That's why I ask at StackOverFlow, instead. And always "ALMOST" get the answer... and then right when I'm about to find out how to do something... I get pointed to 1000s of pages of docs to read. And in this case... they CAN'T be read. Ugh.
Bonnie
@Kimberly - my point in posting that link was that it appeared from your questions that you were unfamiliar with how view controllers work. If that is not the case, then my apologies. All you have to do is override `viewWillAppear` and `viewDidDisappear` in your `UIViewController` subclass, and they will be invoked automatically.
Dave DeLong
A: 

Well, your talking about views so:

viewDidLoad / viewWillLoad
viewDidUnload / dealloc

Seem like good candidates. Also, in your init methods, especially if you want to initialize iVars at that point to something from NSUserDefaults.

Mr-sk
Those were my original thoughts too, but it's possible she wants this to happen *every* time the view is shown/disappears.
Dave DeLong
Yeah, your right - I guess it really depends on the OP's needs.
Mr-sk