DetailViewController在创建新对象时不显示

时间:2012-01-01 22:49:03

标签: uitableview uinavigationcontroller ios5 nsmanagedobject pushviewcontroller

快速回答问题... IBAction按钮正在创建一个新对象,但是当导航器推送DetailViewController时它会给我SIGABRT。 现在我想知道我是否只是不能在那里使用导航控制器功能。 以下代码是无法正常工作的代码

- (IBAction)insertNewObject
{
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];

Event *newEvent = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
DetailViewController *detailController = [[[DetailViewController alloc] init] autorelease];

[newEvent setValue:[NSDate date] forKey:@"timeStamp"];

// Save the context with the new object.
NSError *error = nil;
if (![context save:&error]) {
    //improve this
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

[detailController setEvent:newEvent];

NSLog(@"Detail Controller New Event 1 (from Master) : %@", newEvent);

[self.navigationController pushViewController:detailController animated:YES];
}

现在,当我选择单元格时,它可以完美运行。所以下面的代码有效:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"selectSegue"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

    Event *event = (Event *)[fetchedResultsController objectAtIndexPath:indexPath];
    NSLog(@"Event on Master:: %@", event);

    DetailViewController *detailvc = [[[DetailViewController alloc] init] autorelease];

    detailvc.event = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSLog(@"Event on Detail when created :: %@", detailvc.event);

    if (event) [[segue destinationViewController] setEvent:(Event *) [self.fetchedResultsController objectAtIndexPath:indexPath]];  //was: setDetailItem:selectedObject
    [event release];
    [detailvc.event retain];

}
}

感觉我错过了一些简单的事情。但是,任何人都可以帮助我将对象添加到DetailViewController而不获取SIGABRT吗? 感谢大家新年快乐。

1 个答案:

答案 0 :(得分:0)

您的代码似乎一切正常。 根据我的经验,您应该检查是否为实体设置了所有必需属性。因为在某些情况下,Error语句不介意此错误!