Unable to segue using an accessory indicator

混江龙づ霸主 提交于 2019-12-25 01:39:33

问题


I have a segue with three transitions (see code below). The first from a button. That works perfectly. The second by tapping on the cell in a table view. That one works perfectly too. The third is an accessory button in the tableview cell. This one opens the proper view controller, but does not pass a reference to the patient as I have coded. I have verified this by an NSLog statement in the viewDidLoad of the new view controller and it shows patient as being null.

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

回答1:


The indexPathForSelectedRow will be null when you click on the accessory button because you're not selecting the row. However, the sender argument in prepareForSegue:sender: will be the cell that the accessory button is contained in. So, you should use the following method to get the indexPath (notice that I changed the typing of the argument to UITableViewCell from id):

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


来源:https://stackoverflow.com/questions/16680828/unable-to-segue-using-an-accessory-indicator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!