How to save and load UITextField using NSUserDefaults?

pixelbitlabs picture pixelbitlabs · Sep 6, 2011 · Viewed 7.7k times · Source

Possible Duplicate:
How would I save and load a UITextField?

I have tried EVERYTHING and unfortunately nothing works for me. I have probably tested about 10 ways of saving and loading the UITextFields but they don't work. I have six UITextFields and want to save them when a button is pressed and load when viewDidLoad is called.

Please can I have some help as I've got to publish the app within the next day or two and I still haven't managed to do this...

Thanks,

James

Answer

mayuur picture mayuur · Sep 6, 2011
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

NSString* textField1Text = textField1.text;
[defaults setObject:textField1Text forKey:@"textField1Text"];

NSString* textField2Text = textField2.text;
[defaults setObject:textField2Text forKey:@"textField2Text"];

NSString* textField3Text = textField3.text;
[defaults setObject:textField3Text forKey:@"textField3Text"];

NSString* textField4Text = textField4.text;
[defaults setObject:textField4Text forKey:@"textField4Text"];

NSString* textField5Text = textField5.text;
[defaults setObject:textField5Text forKey:@"textField5Text"];

NSString* textField6Text = textField6.text;
[defaults setObject:textField6Text forKey:@"textField6Text"];

[defaults synchronize];

NSString* temp = [defaults objectForKey:@"textField1Text"];
NSString* temp = [defaults objectForKey:@"textField2Text"];
NSString* temp = [defaults objectForKey:@"textField3Text"];
NSString* temp = [defaults objectForKey:@"textField4Text"];
NSString* temp = [defaults objectForKey:@"textField5Text"];
NSString* temp = [defaults objectForKey:@"textField6Text"];

NSLog(@"%@,%@,%@,%@,%@,%@",temp1,temp2,temp3,temp4,temp5,temp6);

NEW EDIT

  - (IBAction) saveBtnPresssed : (id) sender
  {

        NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];

        NSString* textField1Text = textField1.text;

        [defaults setObject:textField1Text forKey:@"textField1Text"];

        NSString* temp = [defaults objectForKey:@"textField1Text"];

        NSLog(@"%@",temp);

  }