2013-05-07 12 views
0

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

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

这不是我清楚你在做什么试图做,但在didSelectRowAtIndexPath中执行SegueWithIdentifier和pushViewController很可能是错误的。 “showView1”和“showView2”在哪里(哪些视图控制器)? – rdelmar 2013-05-07 01:41:04

+0

谢谢。没有一起使用pSWI和pVC。分别尝试了两种方法。每个人都有缺陷。 pVC传递了行值并更正了pdf名称,但webView不会加载PDF。没有使用pSWI传输行值。每次都是一样的pdf。 ShowView1是SecondViewController的一部分,它将显示不同的PDF,具体取决于选择FirstViewController的行值(为什么我需要传递的行值)。我放入ShowView2是因为我最终会添加ThirdViewController(现在不是)。任何帮助将不胜感激。 – user2353906 2013-05-07 04:16:06

回答

0

您需要使用prepareForSegue和performSegueWithIdentifier。所以,在performSegueWithIdentifier:发件人:方法,传递选定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 
    } 
} 
+0

谢谢!谢谢!这很完美,并教会了我很多。这是我第一次使用堆栈溢出。我想投票还是评分?你在我的书中获得AAA +++。 – user2353906 2013-05-07 18:31:00

+0

@ user2353906,您可以投票(如果您被允许,我不知道是否需要一定数量的声望点才能这样做),并且您可以通过单击大空心箭头接受答案。 – rdelmar 2013-05-07 19:08:01

+0

会做。再一次......谢谢。 – user2353906 2013-05-08 05:25:54