DetailViewController 在创建新对象时不显示
Posted
技术标签:
【中文标题】DetailViewController 在创建新对象时不显示【英文标题】:DetailViewController not showing when creates new object 【发布时间】:2012-01-01 22:49:03 【问题描述】:小伙伴们快速提问... 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】:您的代码似乎一切正常。 根据我的经验,您应该检查是否为实体设置了所有强制属性。因为在某些情况下,错误语句并不介意这个错误!
【讨论】:
所有属性都标记为可选。我认为故事板让我感到困惑。现在有很多方法可以推送视图控制器。但是感谢您迄今为止的帮助。以上是关于DetailViewController 在创建新对象时不显示的主要内容,如果未能解决你的问题,请参考以下文章
UICollectionView 到 DetailViewController
带有 DetailViewController 的 SIGABRT 信号 [重复]
如何从 UISearchBarDisplayController 结果到 detailViewController
iOS拆分视图控制器-在detailViewController中更改图像
performSegueWithIdentifier 替换 detailViewController 有效,但没有调用 prepareForSegue