多个Segue和号码传递不起作用

时间:2013-05-07 00:57:31

标签: ios

我有代码打印出不同的PDF,具体取决于选择的表行。我使用StoryBoards开发了这个并使用Segues。我从prepareForSegue开始,并转移到performSegueWithIdentifier,因为我需要一个TableViewController中的几个segue。我可以:(1)获取传递的行值(整数),但是webView pdf加载不起作用(使用vc2代码),或者(2)我可以加载webView pdf,但不能传递行值performSegueWithIndentifier(所以只有行= 0 pdf加载)。经过一周的研究和尝试,我仍然感到困惑。这里有一些基本的东西,我不明白。请帮忙。

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]];

1 个答案:

答案 0 :(得分: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
    }
}