无法使用附件指标Segue公司 [英] Unable to segue using an accessory indicator
问题描述
我有三个转变(见下文code)一SEGUE。从按钮的第一个。这完美的作品。通过点击在表视图单元格中的第二个。一个完美的作品了。三是在实现代码如下细胞作为附件按钮。这一次开辟了正确的视图控制器,但不传递给患者的参考,因为我有codeD。我已经在新的视图控制器的viewDidLoad中的声明的NSLog验证了这个,它显示病人为被空。
- (无效)prepareForSegue:(UIStoryboardSegue *)赛格瑞发件人:(ID)发送{ 如果([SEGUE符] isEqualToString:@addPatient]){ //添加一个新的病人(选择SEGUE) UINavigationController的* navigationController = segue.destinationViewController; RXAddPatientViewController * addPatientViewController =(RXAddPatientViewController *)navigationController.topViewController; 患者* addPatient = [NSEntityDescription insertNewObjectForEntityForName:@病人inManagedObjectContext:自managedObjectContext]]; addPatientViewController.addPatient = addPatient;
} 如果([SEGUE符] isEqualToString:@,以prescriptions]){ //查看所选病人prescriptions(选择SEGUE) RX prescriptionsViewController * prescriptionViewController = [Segue公司destinationViewController] NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow] 病人* selectedPatient =(病人*)[self.fetchedResultsController objectAtIndexPath:indexPath]; prescriptionViewController.selectedPatient = selectedPatient; 的NSLog(@选定的患者是%@,selectedPatient.patientFirstName); } 如果([SEGUE符] isEqualToString:@editPatient]){ //编辑选定的患者(附件动作) UINavigationController的* navigationController = segue.destinationViewController; RXEditPatientViewController * editPatientViewController =(RXEditPatientViewController *)navigationController.topViewController; NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow] 病人* editPatient =(病人*)[self.fetchedResultsController objectAtIndexPath:indexPath]; //传递一个参考editPatientViewController
editPatientViewController.editPatient = editPatient; 的NSLog(@选定病人%@,editPatient.patientFirstName);
}
}
该indexPathForSelectedRow将是空的,当你在附件按钮点击,因为你不选择行。然而,在prepareForSegue发件人的说法:发件人:将是附件按钮包含在细胞所以,你应该用下面的方法来获取indexPath(请注意,我是从改变参数的打字的UITableViewCell。 ID):
- (无效)prepareForSegue:(UIStoryboardSegue *)赛格瑞发件人:(*的UITableViewCell)发送{ 如果([SEGUE符] isEqualToString:@editPatient]){ //编辑选定的患者(附件动作) UINavigationController的* navigationController = segue.destinationViewController; RXEditPatientViewController * editPatientViewController =(RXEditPatientViewController *)navigationController.topViewController; NSIndexPath * indexPath = [self.tableView indexPathForCell:寄件人]; 病人* editPatient =(病人*)[self.fetchedResultsController objectAtIndexPath:indexPath]; //传递一个参考editPatientViewController
editPatientViewController.editPatient = editPatient; 的NSLog(@选定病人%@,editPatient.patientFirstName);
}
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);
}
}
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);
}
这篇关于无法使用附件指标Segue公司的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!