0

In my app, plist files are being saved to the documents directory, each file name is the current date+time.

I'm loading the list of files from the documents directory to a table: filesTableVC.m.

Each time I want to load the chosen file to a new class: oldFormVC.m but this class is opened empty.

I'm not sure where the problem is.

filesTableVC.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    oldFormVC *oldForm = [[oldFormVC alloc] initWithNibName:nil bundle:nil];

    //load previous data:
    // get paths from root direcory
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:[fileList objectAtIndex:indexPath.row]];

    // check to see if Data.plist exists in documents
    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
    {
        // if not in documents, get property list from main bundle
        plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    }

    // read property list into memory as an NSData object
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    // convert static property liost into dictionary object
    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
    if (!temp)
    {
        NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
    }

    // assign values
    self.theDate = [temp objectForKey:@"theDate"];
    self.theTime = [temp objectForKey:@"theTime"];
    self.carNumber = [temp objectForKey:@"carNumber"];
    self.driverName = [temp objectForKey:@"driverName"];

    [self presentViewController:oldForm animated:YES completion:nil];
}

oldFormVC.m:

- (void)viewDidLoad
{
    [super viewDidLoad];

    dateField.text = theDate;
    timeField.text = theTime;
    carNumberField.text = carNumber;
    driverNameField.text = driverName;
}
DMK
  • 2,448
  • 1
  • 24
  • 35
Arkins
  • 41
  • 4

1 Answers1

2

Write your code in viewDidAppear

- (void)viewDidAppear:(BOOL)animated
{
    dateField.text = theDate;
    timeField.text = theTime;
    carNumberField.text = carNumber;
    driverNameField.text = driverName;
}

Also, change this code in filesTableVC.m:

oldForm.dateField = [temp objectForKey:@"theDate"];
oldForm.theTime = [temp objectForKey:@"theTime"];
oldForm.carNumber = [temp objectForKey:@"carNumber"];
oldForm.driverName = [temp objectForKey:@"driverName"];

Hope it helps you

P.J
  • 6,547
  • 9
  • 44
  • 74