我有一个对象数组。我想选择那些子组并将其传递给detailView。
我在使用didSelectRowAtIndexPath或prepareForSegue
之间陷入困境这是didSelect的主要代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *tempArray = [NSMutableArray new];
tempArray = [mainArray objectAtIndex: indexPath.row];
NSString *numberString = [tempArray valueForKey:@"number"];
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"MyEntity" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"number =[c] %@",numberString];
[request setPredicate:predicate];
[request setIncludesSubentities:NO];
NSError *error = nil;
NSArray *array = [context executeFetchRequest:request error:&error];
selectedObjects = [array mutableCopy];
NSLog(@"selectedObjects: %@", selectedObjects); << -- this looks ok
detailsViewController = [[DetailsViewController alloc] init];
detailsViewController.selectedObjects = selectedObjects;
[self performSegueWithIdentifier:@"mainToDetails" sender:self.view];
}
在此示例中,selectedObjects显示正常。当我到达detailView时,selectedObjects为null。
NSIndexPath *path = [mainTable indexPathForSelectedRow];
NSMutableArray *tempArray = [NSMutableArray new];
tempArray = [main objectAtIndex: path]; << this isn't right.
感谢任何帮助。
答案 0 :(得分:1)
它是零,因为您使用从未出现在屏幕上的alloc init创建了一个控制器实例。由于您正在使用segue,segue会创建新实例,您应该在prepareForSegue中传递数据,您可以使用segue.destinationViewController获取对此控制器的引用。
答案 1 :(得分:1)
如果你想使用didSelectRowAtIndexPath替换这一行
detailsViewController = [[DetailsViewController alloc] init];
以下行
detailsViewController = [self.storyBoard instantiateViewControllerWithIdentifier:@"YOUR STORYBOARD_ID"];
detailsViewController.selectedObjects = selectedObjects;
[self.navigationController pushViewController:detailsViewController animated:YES];