Search code examples
iosswiftcore-datatextviewsaving-data

How to save text data


I am trying to save text data from my TextView. When my cell in an UITableView is clicked, it leads to the next UIViewController where my TextView is set. After writing a note there, going back to my first ViewController and then going back to the ViewController where my TextView is set, the note I wrote does not remain there. I have no idea how to save the data permanently there. I assume Core data or NSDefaults is necessary to do this, but do not know how to implement the code. I appreciate if you could give me the actual code.


Solution

  • When you dismiss or pop the ViewController, the controller will destory. So use NSUserDefault is a good way to save text into sandbox.

    When you exit the textView:

     NSUserDefaults *saver = [NSUserDefaults standardUserDefaults];
    [saver setObject:self.textView.text forKey:@"FirstTextKey"];
    

    Then get into textView controller you want to see the text you have saved:

     NSUserDefaults *saver = [NSUserDefaults standardUserDefaults];
    NSString *firstText = [saver objectForKey:@"FirstTextKey"];