0

我有一个包含三个转换的 segue(参见下面的代码)。第一个来自按钮。这完美无缺。第二个方法是点击表格视图中的单元格。那个也很完美。第三个是 tableview 单元格中的辅助按钮。这会打开正确的视图控制器,但不会像我编码的那样将引用传递给患者。我已经通过新视图控制器的 viewDidLoad 中的 NSLog 语句验证了这一点,它显示耐心为空。

-(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);
    }
}
4

1 回答 1

2

当您单击附件按钮时, indexPathForSelectedRow 将为空,因为您没有选择行。但是,prepareForSegue:sender: 中的 sender 参数将是包含附件按钮的单元格。因此,您应该使用以下方法获取 indexPath(请注意,我将参数的类型从 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);
}
于 2013-05-21T23:17:52.500 回答