将数据从plist加载到另一个类

时间:2013-01-03 10:28:26

标签: iphone objective-c uitableview plist loaddata

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

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

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

我不确定问题出在哪里。

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;
}

1 个答案:

答案 0 :(得分: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"];

希望它可以帮到你