0

我正在显示我的managedObjectContext. 当我退出应用程序并再次打开它时(顺便说一句。正确的术语是什么,我认为重启是另一回事),最后一个视图仍然打开并且标签仍然被填充。一切看起来都很好。滚动此视图后,表格单元格被重新加载,现在所有标签都只显示null值。

我相信我的对象在重新启动后是 {nil},但我不知道为什么。是否有可能手动缓存对象?

4

1 回答 1

0

我找到了一个解决方案:添加一个属性并将数据放入此属性,然后再将数据提供给 DetailsView。

前:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyObject* myObj = (MyObject *)[self.managedObjectContext existingObjectWithID:[self.searchResults objectAtIndex:indexPath.row] error:nil];
    DetailsViewController *details = [[DetailsViewController alloc] init];
    details.myObject = myObj;
    [self.navigationController pushViewController:details animated:YES];
}

后:

@property (nonatomic, strong) MyObject* myObj;

我为我的对象添加了一个属性并将其添加到didSelectRowAtIndexPath方法中。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.myObj = (MyObject *)[self.managedObjectContext existingObjectWithID:[self.searchResults objectAtIndex:indexPath.row] error:nil];
    DetailsViewController *details = [[DetailsViewController alloc] init];
    details.myObject = self.myObj;
    [self.navigationController pushViewController:details animated:YES];
}
于 2013-04-12T13:13:41.143 回答