我有以下三种方法供您查看:
https://gist.github.com/emilevictor/6e01e9c30ce38e9e8a3f
第一种方法" actionsheet:clickedButtonAtIndex:"创建TreatmentLaser,TreatmentPASI或TreatmentSurgery。保存这些实体时,始终会调用委托(方法3)。这一切都是顺利完成的。
第二种方法" onClickNewConsultation:",做同样的事情 - 这次有两个实体,Consult和ClinicalExam。它们彼此相关。第一次加载VC并通过按钮调用此方法时,将调用委托方法。然而,每次从那时起,事实并非如此。有谁知道为什么会出现这种情况?
编辑:
此处添加了代码,以防删除要点:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSInteger adjustedIndex = 0;
if (!_laserEnabled) {
adjustedIndex++;
if (!_pasiEnabled)
adjustedIndex++;
}
else if (!_pasiEnabled && buttonIndex > 0) {
adjustedIndex++;
}
// if (!_laserEnabled && adjustedIndex > 2)
// adjustedIndex++;
// }
switch (buttonIndex + adjustedIndex) {
case 0:
{
TreatmentLaser *newTreatment = [NSEntityDescription insertNewObjectForEntityForName:@"TreatmentLaser" inManagedObjectContext:SCDataManager.app.managedObjectContext];
newTreatment.date = [NSDate date];
newTreatment.patient = _patient;
NSError *error = nil;
// Save the object to persistent store
if (![SCDataManager.app.managedObjectContext save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
return;
}
break;
}
case 1:
{
TreatmentPASI *newTreatment = [NSEntityDescription insertNewObjectForEntityForName:@"TreatmentPASI" inManagedObjectContext:SCDataManager.app.managedObjectContext];
newTreatment.date = [NSDate date];
newTreatment.patient = _patient;
NSError *error = nil;
// Save the object to persistent store
if (![SCDataManager.app.managedObjectContext save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
return;
}
break;
}
case 2:
{
TreatmentSurgery *newTreatment = [NSEntityDescription insertNewObjectForEntityForName:@"TreatmentSurgery" inManagedObjectContext:SCDataManager.app.managedObjectContext];
newTreatment.date = [NSDate date];
newTreatment.patient = _patient;
SurgerySites *surgerySite = [NSEntityDescription insertNewObjectForEntityForName:@"SurgerySites" inManagedObjectContext:SCDataManager.app.managedObjectContext];
surgerySite.treatmentSurgery = newTreatment;
NSError *error = nil;
// Save the object to persistent store
if (![SCDataManager.app.managedObjectContext save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
return;
}
break;
}
default:
break;
}
}
方法2:
- (IBAction)onClickNewConsultation:(UIButton *)sender {
Consult *newConsult = [NSEntityDescription insertNewObjectForEntityForName:@"Consult" inManagedObjectContext:SCDataManager.app.managedObjectContext];
newConsult.date = [NSDate date];
newConsult.patient = _patient;
ClinicalExam *clinicalExam = [NSEntityDescription insertNewObjectForEntityForName:@"ClinicalExam" inManagedObjectContext:SCDataManager.app.managedObjectContext];
clinicalExam.consult = newConsult;
NSError *error = nil;
// Save the object to persistent store
if (![SCDataManager.app.managedObjectContext save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
return;
}
// [self update];
}
委托方法(3):
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
if (_consultsFRC == controller) {
if (type == NSFetchedResultsChangeInsert) {
[_consultTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:newIndexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[_consultTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:newIndexPath.row inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[self performSegueWithIdentifier:@"toConsult" sender:self];
}
else if (type == NSFetchedResultsChangeDelete) {
//[_consultTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
else if (_treatmentLasersFRC == controller) {
if (type == NSFetchedResultsChangeInsert) {
[_consultTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:newIndexPath.row inSection:1]] withRowAnimation:UITableViewRowAnimationAutomatic];
[_consultTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:newIndexPath.row inSection:1] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[self performSegueWithIdentifier:@"toTreatmentLaser" sender:self];
}
else if (type == NSFetchedResultsChangeDelete) {
//[_consultTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:1]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
else if (_treatmentPASIsFRC == controller) {
if (type == NSFetchedResultsChangeInsert) {
[_consultTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:newIndexPath.row inSection:2]] withRowAnimation:UITableViewRowAnimationAutomatic];
[_consultTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:newIndexPath.row inSection:2] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[self performSegueWithIdentifier:@"toTreatmentPASI" sender:self];
}
else if (type == NSFetchedResultsChangeDelete) {
//[_consultTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:2]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
else if (_treatmentProFormasFRC == controller) {
if (type == NSFetchedResultsChangeInsert) {
[_consultTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:newIndexPath.row inSection:3]] withRowAnimation:UITableViewRowAnimationAutomatic];
[_consultTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:newIndexPath.row inSection:3] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
[self performSegueWithIdentifier:@"toTreatmentProForma" sender:self];
}
else if (type == NSFetchedResultsChangeDelete) {
[_consultTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:3]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
// [self performSegueWithIdentifier:@"toTreatmentLaser" sender:self];
}