Owen Bridges
Owen Bridges

Reputation: 81

How do I save the text that the user enters into a UITextView

I am basically making a simple notes application. In myViewController I have a UITextView. I want the text that the user types in the UITextView to be saved so that the next time the user opens the app it is still there. How would I go about doing that? I want it to save onto the device so they can type and save a large amount of text.

Upvotes: 0

Views: 2319

Answers (2)

Tejvansh
Tejvansh

Reputation: 676

You could save it easily using NSUserDefaults too.

Add this line of code in your button save event or can add it in your UITextViewDelegate method textViewDidEndEditing: But don't forget to assign delegate of textview using

textView.delegate = self;

Now to store use following code:

NSString *valueToSave = textView.text;
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
[[NSUserDefaults standardUserDefaults] synchronize];

And to retrieve it back later, use this in your viewDidLoad: or viewWillAppear:

NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"preferenceName"];
textView.text = savedValue;

Hope it helps.. :)

Upvotes: 2

Jonah
Jonah

Reputation: 17958

There are several mechanisms available from iOS for storing data. Which one makes sense for you depends on the size and type of data you want to store and how you need to be able to use it.

A simple place to start might be NSString's writeToFile: (and also Where You Should Put Your App’s Files).

Upvotes: 1

Related Questions