开发者

Crash when presenting modal view controller

开发者 https://www.devze.com 2023-03-22 14:37 出处:网络
In my app I have a function that presents a modal view controller when a bar button is pressed. Sometimes I have to call the same function progrmatically. But, whenever I have to present the view prog

In my app I have a function that presents a modal view controller when a bar button is pressed. Sometimes I have to call the same function progrmatically. But, whenever I have to present the view programatically, it crashes. I have determined that this is the line of code that makes it crash.

[self presentModalViewController:controller animated:YES];

And I get the error

 *** Term开发者_如何学Pythoninating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Website''

even though I am not inserting a new object into my entity.

EDIT: Here is the function that is called programatically and when the bar button is pressed.

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

And this is code where the error occurs.

- (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_;

}

Edit: I am calling the function presentController from the appDelegate when my application resumes from the background state, so I tried calling the same function in my viewController's viewDidLoad function, and it does not crash.


Are you sure that you are initializing the controller before you try to present it?

Also, make sure all your outlets are connected correctly if you're using a nib.


Based on the fragment of the error message, the problem is with a line that contains the:

+[NSEntityDescription entityForName:inManagedObjectContext:]

... method.

Most likely, it's were you get the entity to provide an entity for a fetch request.


I used a NSNotificationCenter to send a message to my viewController to tell it to call the method presentController which works when sending the notification from my appDelegate.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号