views:

46

answers:

2

Hey,

in my app I want to save the text that was typed in in a UITextView. How can I do this?

Thanks for you help.

Leon

+1  A: 

NSString *theText = myTextView.text;

Thats how to get the text from it. You can save it in a variety of places, but where do you want to save it? The user defaults? Core Data? A plaintext file? A properties file? An xml file? In a web app?

Peter DeWeese
I want to save it with the user defaults. I never worked with NSUserDefaults before. I made a textview called "view" and wrote this method: -(IBAction)saveData { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:view.text forKey:@"fieldKey"]; [defaults synchronize]; } and wrote: view.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"fieldKey"]; in my viwdidload but when I open the app only a white screen appears.
Leon
That code for the defaults looks fine. Are there any errors? Did you create the view from a xib or manually? If it is in the xib did you map the text view to the IBOutlet ivar? Put more code in your original question so you can format it (as opposed to comments).
Peter DeWeese
You might try adding something like this to see if you are actually retrieving anything from NSDefaults: NSLog(@"retrieved text = '%@'", [[NSUserDefaults standardUserDefaults] objectForKey:@"fieldKey"]);
westsider
A: 

Create a dictionary and add your text with the key to the dictionary

NSDictionary *dict =  [[NSMutableDictionary alloc] init];
NSString *str = myTextView.text;
[dict setObject:str forKey:@"theText"];

Register your dictionary to NSUserDefaults

[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
[[NSUserDefaults standardUserDefaults] synchronize];
[dict release];

Retrieving it in your whole app:

NSString *theText = [[NSUserDefaults standardUserDefaults] stringForKey:@"theText"];
dianz