UIWebView无法加载

时间:2012-03-28 15:04:25

标签: xcode ios5 uiwebview tableview

我知道这是一个如此简单的问题,但我一直在努力解决这个问题。我试图让UITableView在选择特定行时打开URL。使用NSLog我可以看到urlSelected变量正确设置并正确传递给UrlViewController,但它只是没有显示webview。它只显示带黑屏的导航菜单。如果我在两个视图控制器之间添加推送关系,那么ViewController会正确加载,但是将urlSelected变量传递给UrlViewController时会遇到问题。传递此变量的正确方法是什么,还是需要在两个ViewControllers之间设置不同的关系?这是我目前的情况:

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

听起来您正在使用包含RootViewController和UrlViewController视图的故事板。如果是这种情况,您需要使用故事板实例化UrlViewController:

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

使用它而不是如何在tableView:didSelectRowAtIndexPath:方法中为webController分配init。此外,在包含这两个视图控制器视图的故事板中,将Interface Builder中Attributes检查器的View Controller部分下的Identifier字段设置为字符串UrlViewController。这样做的原因而不仅仅是alloc init是因为描述UrlViewController视图的文件位于故事板内部,所以分配初始化UrlViewController意味着它不会找到它的关联视图,只显示黑屏。 / p>

您也可以使用推送关系执行此操作。您只需设置从RootViewController中的单元格到UrlViewController的推送关系(segue),而不是tableView:didSelectRowAtIndexPath:方法。在RootViewController.m文件中,添加以下方法:

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

这将要求您将Push segue的标识符设置为有效。在故事板中选择表示Push segue的箭头,并将其Identifier设置为字符串PassUrlToUrlViewControllerSegue。

您可以使用在prepareForSegue:sender中为您提供的sender参数来使其动态化,以识别选择了哪个单元格,从而确定要传递的URL。