1

I am working on an iPhone application using Xcode.

I have created a view controller that contains a textview where I hope the user will be able to enter text that can be saved.

As of right now, this is the code I have for this view controller.

  -(void)viewDidLoad
{
   [super viewDidLoad];
   self.navigationItem.title = @"Notes";
   self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

 -(void)viewWillAppear:(BOOL)animated{
    self.notesText.text = _notesText.text;
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    self.notesText.editable = editing;
    [self.navigationItem setHidesBackButton:editing animated:YES];

    if (!editing) {
        _notesText.text = self.notesText.text;

    }

The code that you see allows me to save the data but only if the applications stays opened. I can save text and the data will appear and show if the application stays opened. If I save text and close the app, when I open the app again the data does not appear, so Im assuming its not saving it.

Does anybody know why this is happening. What should I change or add?

Any help would be appreciated!

rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

1

I'm not clear what you're asking.

  • If you want to save the text somewhere permanently, you should use something like Core Data or NSUserDefaults.
  • If you just want to keep the reference to the text in the notes, create an NSString property and use that in your viewWillAppear:. Save what's on the screen in something like viewWillDisappear:.
  • If you just want the text to stay on the screen when the app is backgrounded, you don't have to do anything.
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
  • I want the text entered by the user to be saved and to stay in the app even after the application is closed. (right now the data does not appear if the application is completely closed) – begn.progrm. May 30 '14 at 01:01
  • Then save the data in NSUserDefaults or Core Data. – Enrico Susatyo May 30 '14 at 01:02
  • Im sorry to be a bother. Im pretty new with this language and this type of programming, do you know of any tutorials and such where I can learn how to use these that you mentioned. – begn.progrm. May 30 '14 at 01:09
  • It's a single line of code. `[[NSUserDefaults standardDefaults] setObject:yourText forKey:@"someKeyToRetrieveItWithLater"];` – nhgrif May 30 '14 at 01:14
0

If you want to permanently save the text you will have to save to a file. As mentioned NSDefaults is an option but you can also simply write the string as a file, especially if it's long.

If you wish to simply hold onto the text even if the view is closed you will have to retain the string with the root ViewController

scottdev
  • 141
  • 2
0

If you need to dump your data into a file, you can do the following:

+(void)createFile:(NSString*)receivedString
{
    //Get the file path. Then create a new file if one doesn't exist already or append to it if it does exist
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"Filename.txt"];


    //create file if it doesn't exist
    if(![[NSFileManager defaultManager] fileExistsAtPath:fileName])
        [[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil];

    //append text to file 
    NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath:fileName];
    [file seekToEndOfFile];
    [file writeData:[receivedString dataUsingEncoding:NSUTF8StringEncoding]];
    [file closeFile];
}

If however, the data is small, NSUserDefaults can be used.

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setObject:<you text view data> ForKey:@"yourKey"];
StudentX
  • 2,506
  • 5
  • 19
  • 28
  • If I put the data into a file. How do I load again the file back into the textview? – begn.progrm. May 30 '14 at 11:27
  • You shall have to write a method to read the text file that you created, copy its contents into a NSString object, and then set the NSString object to your textview. Something like, self.myTextView=stringObj; You can refer to this answer: http://stackoverflow.com/a/6546616/2376667 – StudentX May 31 '14 at 10:21