我正在尝试将托管对象的现有属性值传递给模态 segue 视图进行编辑。从“编辑模式”开始,我将从 UITableViewController 子类 (BreadRackTableViewController),通过嵌入式 UINavigationController 使用模态 segue 到 UIViewController 子类 (EditBreadRackTableViewController)。正常的 TableViewCell 选择会推送到另一个 UITableViewController 子类,但这暂时不重要。
我已将属性添加到我的编辑视图控制器标题(和@synthesized)中,以用作吸气剂:
@property (nonatomic, strong) id existingBreadRackDataName;
@property (nonatomic, strong) id existingBreadRackDataDetails;
我要做的是用现有的核心数据实体属性填充编辑视图的字段,这些属性本来是由用户先前设置的(理论上)。如果我传递一个文字字符串值,我可以让数据显示出来,但我需要检索并传递现有数据。
这是我目前拥有的:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"editBreadRackSegue"])
{
EditBreadRackTableViewController *ebrtvc = (EditBreadRackTableViewController *)[segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:indexPath];
// [ebrtvc setBreadRackClass:breadRackClass];
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:@"breadRackDataDetails"];
}
}
最后两行代码...
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:@"breadRackDataDetails"];
...他们都返回零。但是,如果我指定一个文字字符串,该文字字符串会显示在它需要的位置。
说了这么多,我的问题是:
如何检索该选定行(或仅 textLabel 和 detailTextLabels)的属性键值,并将其分配给我的设置器?
解决方案:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"editBreadRackSegue"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:indexPath];
[segue.destinationViewController setExistingBreadRackDataName:[breadRackClass valueForKey:@"breadRackDataName"]];
[segue.destinationViewController setExistingBreadRackDataDetails:[breadRackClass valueForKey:@"breadRackDataDetails"]];
}
// code for other segues here...
}
我最终删除了 ebrtv 变量,并稍微清理了代码。但是,在为 breadRackClass 声明我的indexPath时,使用的是indexPathForCell:sender而不是indexPathForSelectedRow。