gpt4 book ai didi

ios - 无法使用辅助指示器进行 segue

转载 作者:行者123 更新时间:2023-11-28 22:33:51 25 4
gpt4 key购买 nike

我有一个带有三个转换的 segue(见下面的代码)。第一个按钮。这非常有效。第二个是点击表格 View 中的单元格。那一个也很完美。第三个是表格 View 单元格中的附件按钮。这个打开了正确的 View Controller ,但没有像我编码的那样将引用传递给患者。我已经通过新 View Controller 的 viewDidLoad 中的 NSLog 语句验证了这一点,它显示 patient 为 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);
}
}

最佳答案

当您单击附件按钮时,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);
}

关于ios - 无法使用辅助指示器进行 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16680828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com