How can I retain the data stored in plist from UITextField when application is restarted ?

Posted by srikanth rongali on Stack Overflow See other posts from Stack Overflow or by srikanth rongali
Published on 2010-04-21T13:10:22Z Indexed on 2010/04/21 13:13 UTC
Read the original article Hit count: 204

I am using the plist to store the data entered in the UITextFields. But, when I restart my application all the data entered previously was deleted. How can I retain the data in the Plist. I have a UITableView and when a cell is touched a view appears with two UITextFields. nameField and descriptionField. I stored the data in this way.

My code is.

-(void)save:(id)sender
{
    indexOfDataArray = temp;  //the cell selected in table view.
    NSString *string1 = [[NSString alloc]init];
    NSString *string2 = [[NSString alloc]init];
    string1 = nameField.text;
    string2 = descriptionField.text;

    NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys:string2, string1, nil];
    [myArray addObject:myDict];  //myArray is NSMutableArray declared in tableViewController class.
    //[myArray insertObject:myDict atIndex:indexOfDataArray];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"tableVideoData.plist"]; 

    [myArray writeToFile:path atomically:YES];

    UIAlertView *alertMesage = [[UIAlertView alloc] initWithTitle: @"Save Alert" message:@"The data entered is saved" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil] ;
    [alertMesage show];
    [alertMesage release];
}  

When I am using the

[myArray insertObject:myDict atIndex:indexOfDataArray];  

It is giving the error

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray insertObject:atIndex:]: index (2) beyond bounds (1)'  

So I used

[myArray addObject:myDict];

Please help me, how can I retain the data in plist. Thank You.

© Stack Overflow or respective owner

Related posts about cocoa-touch

Related posts about iphone-sdk