无法从另一个视图控制器在 WebView 中显示 URL [英] Unable to display URL in WebView from another view controller

查看:27
本文介绍了无法从另一个视图控制器在 WebView 中显示 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要打开 Web 视图的 UITableViewController.

I have a UITableViewController that needs to open a web view.

在我的函数中,我有:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

                LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:@"LinkViewController_iPhone" bundle:nil];

                [linkViewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];

                [self.navigationController pushViewController:linkViewController animated:YES];

}

我已连接所有插座,但没有收到任何警告,但我没有看到页面拉起.

I have connected all outlets and am not getting any warnings however I do not see the page pull up.

但是,如果我进入实际的 LinkViewController 文件并执行以下操作:

However if I go in the actual LinkViewController file and do something like:

- (void)viewDidLoad
{
    [super viewDidLoad];

        NSString *urlAddress = @"http://www.google.com";

    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlAddress];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    //Load the request in the UIWebView.
    [self.webView loadRequest:requestObj]; 
}

一切看起来都很好.我不明白为什么?

everything seems fine. I do not understand why ?

推荐答案

您应该将 URL 属性添加到您的 LinkViewController.然后在您的表视图控制器中执行以下操作:

You should add a URL property to your LinkViewController. Then in your table view controller you do this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LinkViewController *linkViewController = [[LinkViewController alloc] initWithNibName:@"LinkViewController_iPhone" bundle:nil];             
    linkViewController.URL = [NSURL URLWithString:@"http://www.google.com"];

    [self.navigationController pushViewController:linkViewController animated:YES];
}

现在在您的 LinkViewController 中:

Now in your LinkViewController you do:

- (void)viewDidLoad {
    [super viewDidLoad];

    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:self.URL];

    //Load the request in the UIWebView.
    [self.webView loadRequest:requestObj]; 
}

你原来的东西有两个问题:

There are two problems with what you had originally:

  1. 在表视图控制器中,您在初始化之前访问了 webView 属性.这就是什么都没发生的原因.
  2. 您向外界公开了太多 LinkViewController 的实现细节.如果您在应用程序的许多地方使用 LinkViewController,您需要该类的每个用户都知道创建一个 URL,创建一个请求,然后告诉内部 Web 视图开始加载.所有这些逻辑都属于 LinkViewController 内部.现在该类的每个用户只需要知道设置一个简单的 URL 属性.
  1. In the table view controller, you were accessing the webView property before it was initialized. This is why nothing happened.
  2. You were exposing far too much implementation detail of the LinkViewController to the outside world. If you use LinkViewController in many places in your app, you are requiring every user of the class to know to create a URL, create a request, and then tell the internal web view to start loading. All of that logic belongs inside the LinkViewController. Now each user of the class only needs to know to set a simple URL property.

这篇关于无法从另一个视图控制器在 WebView 中显示 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆