2012-03-28 85 views
0

我知道这是一个这样简单的问题,但我一直在努力,它不会工作。我试图让UITableView在选择特定行时打开URL。使用NSLog我能够看到urlSelected变量被正确设置并正确传递给UrlViewController,但它不会显示webview。它只显示黑屏的导航菜单。如果我在两个视图控制器之间添加推送关系,那么ViewController会正确加载,但它无法将urlSelected变量传递给UrlViewController。传递这个变量的正确方法是什么,或者我需要在两个ViewController之间建立不同的关系?以下是我目前有:UIWebView无法加载

RootViewController.m:

#import "RootViewController.h" 
    #import "UrlViewController.h" 
    .......... 
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   *)indexPath 
    { 

     UrlViewController *webController = [[UrlViewController alloc] init]; 


     if(indexPath.row == 0){ 
      webController.urlAddress = @"http://www.yahoo.com"; 
      NSLog(@"urlAddress being passed is:%@", webController.urlAddress); 
      [self.navigationController pushViewController:webController animated:YES]; 

     }else if(indexPath.row == 1){ 
      webController.urlAddress = @"http://www.google.com"; 
      NSLog(@"urlAddress being passed is:%@", webController.urlAddress); 
      [self.navigationController pushViewController:webController animated:YES]; 


     }else{ 
      webController.urlAddress = @"http://www.abc.com"; 
      NSLog(@"urlAddress being passed is:%@", webController.urlAddress); 
      [self.navigationController pushViewController:webController animated:YES]; 

     }  

    } 

UrlViewController.h:

 @interface UrlViewController : UIViewController{ 
      NSString *urlAddress; 
     } 
     @property (strong) IBOutlet UIWebView *webView; 
     @property (nonatomic, retain) NSString *urlAddress; 

UrlViewController.m:

 @implementation UrlViewController 
     @synthesize webView; 
     @synthesize urlAddress; 
     ........... 
    - (void)viewWillAppear:(BOOL)animated { 
     NSURL *url = [NSURL URLWithString:urlAddress]; 
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
     [self.webView loadRequest:requestObj]; 
     NSLog(@"The value being receieved is:%@", urlAddress); 
    } 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     // Do any additional setup after loading the view. 
    } 

我收到下面的输出NSLogs:

2012-03-28 09:53:30.266 BeloitTurner[4278:f803] urlAddress being passed is:http://www.yahoo.com 
    2012-03-28 09:53:30.269 BeloitTurner[4278:f803] The value being receieved is:http://www.yahoo.com 

任何帮助,非常感谢。

+0

我已经回答了这个[这里](http://stackoverflow.com/questions/8791517/pass-a-uiwebview-request-using-prepareforsegue/13723765#13723765)!这是一个安静的问题。 – Ashoor 2012-12-05 13:11:32

回答

0

这听起来像你正在使用一个包含两个RootViewController的和UrlViewController意见的故事板。如果是这样的话,你需要用故事板来实例UrlViewController:

UrlViewController* webController = [self.storyboard instantiateViewControllerWithIdentifier:@"UrlViewController"]; 

使用此,而不是你如何初始化页头的webController在您的tableView:didSelectRowAtIndexPath方法:方法。另外,在包含这两个视图控制器的视图的故事板中,将Interface Builder中属性检查器的视图控制器部分下的标识符字段设置为字符串UrlViewController。这样做而不仅仅是alloc init的原因是,描述UrlViewController视图的文件在storyboard内,因此alloc initl UrlViewController意味着它不会找到它的相关视图,并且只显示一个黑屏。

你也可以用推送关系做到这一点。而不是tableView:didSelectRowAtIndexPath:方法,您只需将RootViewController中单元格的Push关系(一个segue)设置为UrlViewController即可。在你RootViewController.m文件,添加下面的方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"PassUrlToUrlViewControllerSegue"]) { 
     segue.destinationViewController.urlAddress = @"http://www.yahoo.com"; 
    } 
} 

这将需要您设置标识符推SEGUE工作。在故事板中选择表示Push segue的箭头,并将其标识符设置为字符串PassUrlToUrlViewControllerSegue。

你能做出这样的动态通过使用prepareForSegue给你发送参数:发件人:确定哪些细胞被选中,从而得以传承哪个URL。

+0

这就是我所寻找的技巧。非常感谢您的帮助。 – SchoolTechie 2012-03-29 00:55:29