我正在尝试将托管对象的现有属性值传递给模态segue视图以进行编辑。从“编辑模式”,我将从UITableViewController子类(BreadRackTableViewController),通过嵌入式UINavigationController使用模态segue到UIViewController子类(EditBreadRackTableViewController)。普通的TableViewCell选择推送到另一个UITableViewController子类,但这暂时并不重要。
我已经在编辑视图控制器标题(和@synthesized)中添加了属性,以充当getter:
@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)的属性键值,并将其分配给我的setter?
解决方案:
-(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 。
答案 0 :(得分:0)
我认为indexPathForSelectedRow可能是个问题。
您可以将indexPath从didSelectRowAtIndexPath传递到prepareForSegue,例如:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"editBreadRackSegue" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"editBreadRackSegue"])
{
NSIndexPath *clickedIndexPath = (NSIndexPath*)sender;
if( [clickedIndexPath isKindOfClass:[NSIndexPath class]] ){
BreadRackClass *breadRackClass = [[self fetchedResultsController]objectAtIndexPath:clickedIndexPath];
EditBreadRackTableViewController *ebrtvc = (EditBreadRackTableViewController *)[segue destinationViewController];
ebrtvc.existingBreadRackDataName = breadRackClass.breadRackDataName;
ebrtvc.existingBreadRackDataDetails = [breadRackClass valueForKey:@"breadRackDataDetails"];
}
}
}
让我知道它是否适合你。