Reputation: 1034
Ive had a look at many other questions regarding this (I know there are alot!), but none seem to satisfy my query. Basically I have a view controller which includes two UITextFields. The app is designed so that this page loads, the user inputs numbers, and then taps the background to get rid of the keyboard, and the values save. I then want this to be pre loaded in the text field every time the user navigates back to the page. I presume I will have to create another IBAction called saveData or something, but is there anyway to automatically save it when the background is tapped? Also to save the data I was thinking of using NSUserDefaults, are there any other methods to save data? Any help is appreciated, Michael I use the following code for the editing and background tap part of my application:
-(IBAction) textFieldDoneEditing : (id) sender{
[sender resignFirstResponder];
}
-(IBAction) backgroundTap:(id) sender{
[self.textInputOne resignFirstResponder];
[self.textInputTwo resignFirstResponder];
}
Upvotes: 0
Views: 1866
Reputation: 7936
If you want to save the data (especially if the app is closed) NSUserdefaults is a nice and easy way to do it. Other options include writing to your own plist file or setting up a core data store, but you're just saving two numbers so not sure you need all that.
Upvotes: 1
Reputation: 1387
NSUserDefaults will work nicely for this:
-(IBAction) backgroundTap:(id) sender {
NSString *inputOne = textInputOne.text;
NSString *inputTwo = textInputTwo.text;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:inputOne forKey:@"InputOne"];
[userDefaults setObject:inputTwo forKey:@"InputTwo"];
[textInputOne resignFirstResponder];
[textInputTwo resignFirstResponder];
[sender resignFirstResponder];
}
then in - (void)viewDidLoad
call back the stored NSUserDefaults:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
textInputOne.text = [userDefaults objectForKey:@"InputOne"];
textInputTwo.text = [userDefaults objectForKey:@"InputTwo"];
Should work fine
Upvotes: 3