UIWebView似乎未加载页面 [英] UIWebView doesn't seem to be loading the page

查看:45
本文介绍了UIWebView似乎未加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于导航控制器的应用程序,该应用程序下载RSS提要并在TableView中布置项目标题.该部分有效.当我单击文章标题时,我想将FeedArticleController的实例推入导航堆栈. FeedArticleController loadView方法设置视图(因为我想学习如何以编程方式而不是使用IB来执行此操作).这是loadView:

I've got a navigation controller based app that downloads an RSS feed and lays out the item titles in a TableView. That part works. When I click on an article title, I want to push an instance of my FeedArticleController onto the navigation stack. The FeedArticleController loadView method sets up the views (since I want to learn how to do this programmatically instead of using IB). Here's the loadView:

- (void)loadView {
    UIView *view = [[UIView alloc] init];
    self.view = view;

    webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webView.delegate = self;

    [view addSubview:webView];
}

这有意义吗? 现在,要使webView实际加载页面,我有以下viewDiDLoad方法:

Does that make sense? Now, to make the webView actually load the page, I have this viewDiDLoad method:

- (void)viewDidLoad {
    [super viewDidLoad];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]]; 
}

然后我有了委托方法webViewDidStartLoad和webViewDidFinishLoad.他们只是打印到NSLog.

And then I have the delegate methods, webViewDidStartLoad and webViewDidFinishLoad. They just print to NSLog.

我在构造和添加视图时是否缺少某些东西?延迟后,委托方法似乎正在打印到日志中,并且在实例化webView时,线路上存在流量.所以,我认为它正在加载页面,但未显示?

Am I missing something in how I constructed and added the views? The delegate methods seem to be printing to the log after a delay and there is traffic on the line when the webView is instantiated. So, I think it is loading the page, but not being displayed?

谢谢大家.

推荐答案

这里有两件事要注意:

  1. 将Web视图的autoresizingMask设置为UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight,以在调整其父视图的大小时对其进行调整.这是因为默认情况下,您创建的父UIView和UIWebView都具有零帧(0,0,0,0).通过设置此设置,当父母在屏幕上之前调整大小时,网络视图将被调整大小以匹配.
  2. 不要使用单独的UIView实例,只需将self.view设置为您的UIWebView实例.
  1. Setting the web view's autoresizingMask to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight to make the web view resize when its parent is resized. This is because by default both the parent UIView you've created and the UIWebView have zeroed frames (0,0,0,0). By setting this, when the parent is resized prior to going onscreen the web view will be resized to match.
  2. Don't use a separate UIView instance, just set self.view to your UIWebView instance.

除非您确实需要控制器内的其他视图,否则我建议您使用第二个选项.另外,我应该在这里指出,您在上面的代码片段中同时泄漏了UIView和UIWebView.这可以简化,但如果不是这样,则应在分配视图后释放视图.这是实现上述解决方案2的示例:

I'd recommend the second option, unless you really need another view inside your controller. Also, I should point out here that you're leaking both the UIView and the UIWebView inthe code snippet above. That might be simplified, but in case it isn't, you should release the views after assigning them. Here's an example implementing solution 2 above:

- (void) loadView
{
    webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    webView.delegate = self;    // weak reference: doesn't retain self
    self.view = webView;        // strong reference: retains webView
    [webView release];          // release initial reference: now 'owned' through self.view
}

这篇关于UIWebView似乎未加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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