Reputation: 219
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.)
Upvotes: 1
Views: 1117
Reputation: 641
You should always put the code itself into its own file pair to manage the user defaults, and this module should be responsible for serialization and deserialization, though objects that are serialized should own that virtuosity themselves. You get only the settings you absolutely need in viewDid Load, so as not to slow down the initialization.
If you have capacious user settings, arrays and dictionaries, multiple session data, don't make the mistake of storing them under a single dictionary and key. Split them up.
If your app requires users log on (I do crypto so most of mine do) then only after logon, verifying password from a minimal load of user settings, should you go on to load the heavier session settings. This is on "user time" anyway.
Upvotes: 0
Reputation: 13407
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.
Upvotes: 0
Reputation: 243156
How about the viewWillAppear
and dealloc
/viewDidDisappear
methods of that view's UIViewController
?
Upvotes: 3