0

在我的应用程序中,plist文件被保存到文档目录中,每个文件名都是当前日期+时间。

我正在将文件目录中的文件列表加载到表中:filesTableVC.m.

每次我想将所选文件加载到一个新类时:oldFormVC.m 但是这个类是空的。

我不确定问题出在哪里。

文件表VC.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;
}
4

1 回答 1

2

将您的代码写入viewDidAppear

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

此外,在 filesTableVC.m 中更改此代码:

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

希望对你有帮助

于 2013-01-03T10:32:41.003 回答