I am trying to use NSUserdefaults to save some data from a text field, I want it to be saved when the application quits and loaded when the application starts, however I have run into a wall.
The methods that I want to use are all in the AppDelegate, Now I do not understand how to use AppDelegate very well, I have thought of two possible ways to achieve this, I have no idea if it would work though.
Any suggestions would appreciated
Thank you very much for your time
Storing Default Objects The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs.
The app delegate is effectively the root object of your app, and it works in conjunction with UIApplication to manage some interactions with the system. Like the UIApplication object, UIKit creates your app delegate object early in your app's launch cycle so it's always present.
The application delegate is a controller object. By default, it is the owner and controller of the main window -- which is a view -- in an iOS app. The app delegate receives messages from an object representing -- or modeling -- the application itself (an instance of UIApplication ).
At runtime, you use UserDefaults objects to read the defaults that your app uses from a user's defaults database. UserDefaults caches the information to avoid having to open the user's defaults database each time you need a default value.
Rather than keep the text field in your AppDelegate, keep the text. I'd do the following:
1) In AppDelegate.h:
@property (strong, nonatomic) NSString *textToBeSaved;
2) In AppDelegate.m, read and write textToBeSaved to NSUserDefaults when your app launches and terminates. On launch:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
self.textToBeSaved = [defaults objectForKey:@"save_me"];
and, before termination:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:self.textToBeSaved forKey:@"save_me"];
BOOL success = [defaults synchronize];
3) In SomeViewController.m that naturally owns the UITextField, in viewWillAppear:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
myTextField.text = appDelegate.textToBeSaved;
4) When you set the textToBeSaved depends on your UI, but whenever you know the text is ready (say on textFieldShouldReturn, or shouldEndEditing), you can hand the string to AppDelegate:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.textToBeSaved = myTextField.text;
If there's no UI to let the user accept the text, you can save the string on (textField:shouldChangeCharactersInRange:replacementString).
As you mentioned above you have two options:
1-) Directly reach VC's UITextField
from AppDelegate
's applicationDidFinishLaunching
and applicationWillTerminate
methods.
On applicationDidFinishLaunching
:
Read from NSUserDefaults
, set UITextField
in VC.
On applicationWillTerminate
:
Read from UITextField
in VC, set NSUserDefaults
and synchronize
.
2) Create proper methods in VC to do the same job and call them from AppDelegate
.
-(void)record
{
// Read from UITextField, set UserDefaults and synchonize.
}
-(void)restore
{
// Read from UserDefaults, set UITextField.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With