2012-03-28 136 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