0

I have code that prints out different PDFs depending on what table row is picked. I developed this using StoryBoards and use Segues. I started with prepareForSegue, and shifted to performSegueWithIdentifier because I need several segues from one TableViewController. I can either: (1) get the row value (integer) passed, buth the webView pdf load doesn’t work (with the vc2 code), or (2) I can load the webView pdf, but not pass the row value with the performSegueWithIndentifier (so only the row = 0 pdf loads). After a solid week of research and trying, I remained perplexed. There is something fundamental here that I don’t understand. Please help.

FirstViewController

-   (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
    int row = [myIndexPath row];

if (row == 0){
    [self performSegueWithIdentifier:@"showView1" sender:self];}

    else if (row == 1){
        [self performSegueWithIdentifier:@"showView1" sender:self];}

    else if (row == 2){
        [self performSegueWithIdentifier:@"showView2" sender:self];}

SecondViewController *vc2 = [[SecondViewController alloc]init];
vc2.row = row;
[self.navigationController pushViewController:vc2 animated:YES];




SecondViewController

if (row == 0) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cover" ofType:@"pdf"];
        pdfUrl = [NSURL fileURLWithPath:path];
        NSLog(@"in SecondView Controller path =: %@", pdfUrl);}

    else if(row == 1){
        NSString *path = [[NSBundle mainBundle] pathForResource:@"welcome" ofType:@"pdf"];
        pdfUrl = [NSURL fileURLWithPath:path];
        NSLog(@"in SecondView Controller path =: %@", pdfUrl);}

    [webView loadRequest:[NSURLRequest requestWithURL:pdfUrl]];
4

1 回答 1

0

您需要将 prepareForSegue 与 performSegueWithIdentifier 一起使用。因此,在 performSegueWithIdentifier:sender: 方法中,将选定的 indexPath 作为发送者传递:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   if (indexPath.row < 2) [self performSegueWithIdentifier:@"showView1" sender:indexPath];
   if (indexPath.row == 2) [self performSegueWithIdentifier:@"showView2" sender:indexPath];
}

然后,使用 prepareForSegue 传递行信息:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSIndexPath *)selectedIndexPath {
    if ([segue.identifier isEqualToString:@"showView1"]) {
        SecondViewController *vc2 = segue.destinationViewController;
        vc2.row = selectedIndexPath.row;
    }else if ([segue.identifier isEqualToString:@"showView2"]) {
        //do something else
    }
}
于 2013-05-07T04:36:31.573 回答