Search code examples
iphoneiosuitextfieldsave

How to save text temporary in textfield?


I have a little problem, when I have text put in a UITextfield in view controller A and I go to an other view controller and then i go back to view controller A the text in the textfield had disappear.

Is there a way to save the text temporary in the textfield? So the text won't disappear when you go to an other view controller.


Solution

  • when you navigate the page at that time save the data in NSUserDefaults .

    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
        [defaults setObject:textfield.text forKey:@"textfield value"];
        [defaults synchronize];
    

    and re assign the textfield value in viewWillAppear method.

    NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
       textfield.text= [defaults  valueForKey:@"textfield value"];
    

    or

    other wise take an global string and assign the string value to textFiled and Vice Versa.