【问题标题】:Crash when presenting modal view controller呈现模态视图控制器时崩溃
【发布时间】:2011-07-23 00:39:35
【问题描述】:

在我的应用程序中,我有一个功能可以在按下条形按钮时显示模式视图控制器。有时我必须以编程方式调用相同的函数。但是,每当我必须以编程方式呈现视图时,它就会崩溃。我已经确定这是导致它崩溃的代码行。

[self presentModalViewController:controller animated:YES];

我得到了错误

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Website''

即使我没有将新对象插入到我的实体中。

编辑: 这是在按下条形按钮时以编程方式调用的函数。

- (void) presentController {
WebController *webController = [[WebController alloc] initWithNibName:@"WebController" bundle:nil];
webController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
webController.delegate = self;
[self presentModalViewController:webController animated:YES];
[webController release];
 }

这是发生错误的代码。

- (NSFetchedResultsController *)fetchedResultsController {

if (fetchedResultsController_ != nil) {
    return fetchedResultsController_;
}

/*
 Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Website" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];

NSError *error = nil;
if (![fetchedResultsController_ performFetch:&error]) {
    /*
     Replace this implementation with code to handle the error appropriately.

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
     */
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return fetchedResultsController_;

}

编辑:当我的应用程序从后台状态恢复时,我从 appDelegate 调用函数 presentController,因此我尝试在 viewController 的 viewDidLoad 函数中调用相同的函数,但它没有崩溃。

【问题讨论】:

  • 您应该包含整个错误消息。 NSInternalInconsistencyException 涵盖了很多可能性。
  • 我们需要了解更多用于以编程方式调用的代码。问题将隐藏在该代码中的某个地方,因为您说这就是它工作与不工作之间的区别。

标签: iphone core-data uiviewcontroller presentmodalviewcontroller


【解决方案1】:

您确定在尝试展示之前初始化控制器吗?

此外,如果您使用的是笔尖,请确保所有插座均已正确连接。

【讨论】:

  • 我调用的函数与按下条形按钮时调用的函数完全相同,但当我以编程方式调用它时它总是崩溃。
【解决方案2】:

根据错误消息的片段,问题在于包含以下内容的行:

+[NSEntityDescription entityForName:inManagedObjectContext:]

... 方法。

最有可能的是,您让实体为获取请求提供实体。

【讨论】:

  • 它确实说当它崩溃时错误出现在该行,但我不明白为什么,因为错误出现在呈现另一个 viewController 的 viewController 中,而不是正在呈现的那个。
  • 不要把你的逻辑强加给证据。错误就在哪里。您必须至少了解错误及其发生的代码,然后才能排除它。无论如何,如果不查看失败行周围的实际代码,我们将无法为您提供帮助。
  • 此行不插入任何内容,而是从模型中获取实体描述。如果找不到实体或命中重叠实体以及其他原因,它可能会失败。
【解决方案3】:

我使用NSNotificationCenter 向我的viewController 发送一条消息,告诉它调用presentController 方法,该方法在从我的appDelegate 发送通知时起作用。

【讨论】:

    猜你喜欢
    • 2011-06-21
    • 2010-11-27
    • 2017-04-10
    • 2012-03-26
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    相关资源
    最近更新 更多