Take the 2-minute tour ×

I have a text field within my app.

When a user inputs text I need a way to "remember" the value in the text field so that it can be recalled if the user quits the app and restarts it later.

Please can you point me in the right direction?

share|improve this question
For that u have to use any persistence storage method like plist. –  CRDave Sep 21 '12 at 13:06
I am new to the development. Can you give me any links. –  Sharanya Sep 21 '12 at 13:09
CRDave, this probably isn't the best way at all. Its just some text, faffing with plists and writing to files isn't going to be the best option. NSUserDefaults was created for this exact reason. And anything more complicated would be a job for SQLite/CoreData. –  Thomas Clayson Sep 21 '12 at 13:10
U r right But he just want to store a string and for storing a simple string I don't recommend to use a hole data base. –  CRDave Sep 21 '12 at 13:16
add comment

closed as not constructive by DrummerB, Janak Nirmal, J. Steen, Andro Selva, 0x7fffffff Sep 22 '12 at 12:48


3 Answers

up vote 1 down vote accepted

You can use NSUserDefaults to store/retrieve your textfield data.

//To store
[[NSUserDefaults standardUserDefaults] setValue:@"Hello World" forKey:@"anyKey"];
[[NSUserDefaults standardUserDefaults] synchronize];

//To retrieve
yourTextField.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"anyKey"];
share|improve this answer
Thank ou so much.. –  Sharanya Sep 21 '12 at 13:34
add comment

The easiest way to do this would be to use NSUserDefaults

NSUserDefaults are really a way to store settings and configuration options between sessions of an app (e.g. games might use this to "remember" if a user has selected to mute the sound).

You can use it for this however, it won't be a problem at all. Have a read through the class reference I have linked you to above, and come back and ask any questions if you get stuck.

share|improve this answer
I tried this. But there was no effect. NSString *savestring = mileagerate.text; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults objectForKey:@"savedstring"]; [defaults setObject:savestring forKey:@"savedstring"]; [defaults synchronize]; –  Sharanya Sep 21 '12 at 13:23
Thank you Thomas i got it. Thank you for editing my post :) –  Sharanya Sep 21 '12 at 13:35
Cool, glad I could help you. :) No problem - I hate seeing people get downvoted for asking perfectly reasonable questions because its a little bit confusing. Thought you might get more help if I reworded it for you. –  Thomas Clayson Sep 21 '12 at 13:54
add comment

store the value in NSUserDefaults standardDefaults ,the first time you enter the values like this,..

[[NSUserDefaults standardDefaults]setValue:textField.text forKey:@"username"]; //key is user defined



if (! [[[NSUserDefaults standardDefaults]valueForkey:@"username"]isEqualToString:@""]){
        textField.text =(NSString *) [[NSUserDefaults standardDefaults]valueForkey:@"username"];
share|improve this answer
add comment
