无法使用附件指示器继续使用

我有一个三转换(参见下面的代码)。 第一个从一个button。 这是完美的。 第二个是通过点击表格视图中的单元格。 那个也是完美的。 第三个是tableview单元格中的附件button。 这个打开正确的视图控制器,但没有传递给病人的引用,因为我已经编码。 我已经在新的视图控制器的viewDidLoad中的NSLog语句validation了这一点,它显示病人为空。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"addPatient"]) { // to add a new patient (selection segue) UINavigationController *navigationController = segue.destinationViewController; RXAddPatientViewController *addPatientViewController = (RXAddPatientViewController*)navigationController.topViewController; Patient *addPatient = [NSEntityDescription insertNewObjectForEntityForName:@"Patient" inManagedObjectContext:[self managedObjectContext]]; addPatientViewController.addPatient = addPatient; } if ([[segue identifier] isEqualToString:@"toPrescriptions"]) { // to view prescriptions for the selected patient (selection segue) RXPrescriptionsViewController *prescriptionViewController = [segue destinationViewController]; NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; Patient *selectedPatient = (Patient*) [self.fetchedResultsController objectAtIndexPath:indexPath]; prescriptionViewController.selectedPatient = selectedPatient; NSLog(@"Selected Patient is %@", selectedPatient.patientFirstName); } if ([[segue identifier] isEqualToString:@"editPatient"]) { // to edit the selected patient (accessory action) UINavigationController *navigationController = segue.destinationViewController; RXEditPatientViewController *editPatientViewController = (RXEditPatientViewController*)navigationController.topViewController; NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; Patient *editPatient = (Patient*) [self.fetchedResultsController objectAtIndexPath:indexPath]; // passing a reference to the editPatientViewController editPatientViewController.editPatient = editPatient; NSLog(@"Selected patient is %@", editPatient.patientFirstName); } } 

当您点击附件button时,indexPathForSelectedRow将为空,因为您没有select该行。 然而,prepareForSegue:sender:中的发件人参数将是包含附件button的单元格。因此,您应该使用以下方法获取indexPath(注意,我将参数的input从id更改为UITableViewCell):

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UITableViewCell *)sender { if ([[segue identifier] isEqualToString:@"editPatient"]) { // to edit the selected patient (accessory action) UINavigationController *navigationController = segue.destinationViewController; RXEditPatientViewController *editPatientViewController = (RXEditPatientViewController*)navigationController.topViewController; NSIndexPath *indexPath = [self.tableView indexPathForCell:sender]; Patient *editPatient = (Patient*) [self.fetchedResultsController objectAtIndexPath:indexPath]; // passing a reference to the editPatientViewController editPatientViewController.editPatient = editPatient; NSLog(@"Selected patient is %@", editPatient.patientFirstName); }