在我的應用程序中,我有一個功能,當按下條形按鈕時呈現模態視圖控制器。有時我需要編程調用相同的函數。但是,無論何時我必須以編程方式呈現視圖,它都會崩潰。我確定這是使它崩潰的代碼行。當呈現模態視圖控制器時出現故障
[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涵蓋了很多可能性。 – TechZen
我們需要查看更多用於以編程方式調用的代碼。問題會隱藏在代碼的某處,因爲你說這是它工作和不工作的區別。 –