我有一个标签栏控制器界面,其主要概念是创建一个 ID,通过文本字段添加名称,然后通过推送 segue 将多个用户选择的属性添加到视图控制器。
我已经解决了主要添加 ID 视图控制器和推送 segue 以从详细视图控制器添加单个属性,但现在我想修改场景......
在详细视图控制器上,我有一个程序列表,用户可以选择一个传递回主视图的程序...如何将其设置为“复选框”或多选方法并拥有所有程序选择传回主视图而不仅仅是一个?
我使用核心数据作为我的存储模型以及主要实体“MedRec”与构成 MedRec 数据的其他实体之间的关系——“程序”、“级别”、“日期”等。
#import "MedRecTVC.h" #import "MedRec.h" @implementation MedRecTVC @synthesize fetchedResultsController = _fetchedResultsController; @synthesize managedObjectContext = _managedObjectContext; @synthesize selectedMedRec; -(void)setupFetchedResultsController { // 1 - Decide what Entity you want NSString *entityName = @"MedRec"; //Put your entity name here NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName); // 2 - Request the Entity NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName]; // 3 - Filter it if you want //request.predicate = [NSPredicate predicateWithFormat:@"Level.name = Blah"]; // 4 - Sort it if you want request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"number" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]]; // 5 - Fetch it self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; [self performFetch]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setupFetchedResultsController]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"MedRec Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } //configure the cell //decide the order of table cell text and detail text MedRec *medrec = [self.fetchedResultsController objectAtIndexPath:indexPath]; NSString *number = [NSString stringWithFormat:@"%@", medrec.number]; cell.textLabel.text = number; cell.detailTextLabel.text = medrec.inLevel.level1; return cell; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.tableView beginUpdates]; // Avoid NSInternalInconsistencyException // Delete the role object that was swiped MedRec *medrecToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath]; NSLog(@"Deleting (%@)", medrecToDelete.number); [self.managedObjectContext deleteObject:medrecToDelete]; [self.managedObjectContext save:nil]; // Delete the (now empty) row on the table [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self performFetch]; [self.tableView endUpdates]; } } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"Add MedRec Segue"]) { NSLog(@"Setting MedRecTVC as a delegate of AddMedRecTVC"); AddMedRecTVC *addMedRecTVC = segue.destinationViewController; addMedRecTVC.delegate = self; addMedRecTVC.managedObjectContext = self.managedObjectContext; } else if ([segue.identifier isEqualToString:@"MedRec Detail Segue"]) { NSLog(@"Setting MEdRecTVC as a delegate of MedRecDetailTVC"); MedRecDetailTVC *medrecDetailTVC = segue.destinationViewController; medrecDetailTVC.delegate = self; medrecDetailTVC.managedObjectContext = self.managedObjectContext; NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; self.selectedMedRec = [self.fetchedResultsController objectAtIndexPath:indexPath]; NSLog(@"Passing selected medrec (%@) to MedRecDetailTVC", self.selectedMedRec.number); medrecDetailTVC.medrec = self.selectedMedRec; } else { NSLog(@"Unidentified Segue Attempted!"); } } - (void)theSaveButtonOnTheAddMedRecTVCWasTapped:(MedRecDetailTVC *)controller { //refresh the table //close delegated view [controller.navigationController popViewControllerAnimated:YES]; } - (void)theSaveButtonOnTheMedRecDetailTVCWasTapped:(MedRecDetailTVC *)controller { [controller.navigationController popViewControllerAnimated:YES]; } @end