UIWebView 在 ios 中不显示内容 [英] UIWebView do not display contents in ios

查看:27
本文介绍了UIWebView 在 ios 中不显示内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在处理一个需要与 SplitViewController 类似的 GUI 的小项目.现在我所做的是,我有两个 UIViewControllers,即 MasterViewController 和 DetailViewController,它们被添加为 ViewController 的子视图ViewController.m 的代码如下所示

I have been working around one small project which needs a similar GUI as that of SplitViewController. Now what i have done is, I have two UIViewControllers namely MasterViewController and DetailViewController which are added as subview of ViewController The code is shown below for ViewController.m

- (void)viewDidLoad
{  
   UIScreen *screen = [UIScreen mainScreen];
   CGRect fullScreenRect = screen.bounds; // always implicitly in Portrait orientation.
   width = fullScreenRect.size.width;
   height = fullScreenRect.size.height;
   detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
   masterViewController = [[[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil] autorelease];
   masterViewController.view.frame = CGRectMake(0, 0, 299, height);
   detailViewController.view.frame = CGRectMake(300, 0, width-300, height);

  [self.view addSubview:masterViewController.view];
  [self.view addSubview:detailViewController.view];
}

现在在 MasterViewController didLoad 方法中我添加了一个子视图 MasterTableViewController...

Now in the MasterViewController didLoad method i have added as a subview MasterTableViewController...

MasterTableViewController 类看起来像这样...

MasterTableViewController class looks like this...

   @interface MasterTableViewController : UITableViewController 
   {
     DetailViewController *detailViewController;
   }

与 UITableViewController 相关的所有功能都已经完成...我还添加了从用户添加 URL 并将其存储在 MasterTableView 中的功能.现在在下面的函数中,我试图用所需的字符串调用 DetailViewController 的 loadWebView 函数.对函数进行了完美的调用.

All the functions related to UITableViewController are already made... and i have also added the functionality to add URL from user and store it in the MasterTableView. Now in the following function I am trying to call loadWebView function of DetailViewController with the required string. The call is made perfectly to the function.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
   NSString *urlString = [_objects objectAtIndex:indexPath.row];
   detailViewController = [[DetailViewController alloc]init];
   [detailViewController loadWebView:urlString];
 }

在 DetailViewController 的 loadWebView 方法中,我有以下实现..

In the loadWebView method of DetailViewController i have the following implementation..

 -(void) loadWebView: (NSString *) urlToLoad
 {
   self.webView = [[[UIWebView alloc]
                 initWithFrame:CGRectMake(0, 45, self.view.frame.size.width, self.view.frame.size.height)] autorelease];
   [self.webView setDelegate:self];

   NSLog(@"SELF->WEBVIEW: %@",self.webView);
   NSURL *url = [NSURL URLWithString:urlToLoad];
   NSLog(@"loading url %@ %@", urlToLoad, self.webView);
   NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

   [self.webView loadRequest:requestObj];
 }

我还实现了以下功能,以确保我的 Webview 正常工作..

I have also implemented the following function just to make sure that my Webview is working..

 - (void)webViewDidStartLoad:(UIWebView *)webView
 {
    NSLog(@"webViewDidStartLoad");
 }
 - (void)webViewDidFinishLoad:(UIWebView *)webView
 {
    NSLog(@"webViewDidFinishLoad");
 }

现在,当我运行我的程序时,日志会打印出所有这些东西...

Now when i run my program the log prints all these things...

 2012-10-15 20:04:21.621 SyneBlogApp[2320:c07] device width height 768.000000 1024.000000
 2012-10-15 20:04:21.630 SyneBlogApp[2320:c07] calling viewDidLoad for detailView
 2012-10-15 20:04:21.636 SyneBlogApp[2320:c07] /Users/Library/Application Support/iPhone Simulator/5.1/Applications/B846EDA4-49CF-4DD3-AAB3-FF8BC19C65DA/Documents/Data.plist: 
 2012-10-15 20:04:22.942 SyneBlogApp[2320:c07] calling viewDidLoad for detailView
 2012-10-15 20:04:22.954 SyneBlogApp[2320:c07] SELF>WEBVIEW: <UIWebView: 0x6a94510; frame = (0 45; 768 1004); layer = <CALayer: 0x6a94930>>
 2012-10-15 20:04:22.955 SyneBlogApp[2320:c07] loading url http://wordpress.com   <UIWebView: 0x6a94510; frame = (0 45; 768 1004); layer = <CALayer: 0x6a94930>>
 2012-10-15 20:04:22.994 SyneBlogApp[2320:c07] webViewDidStartLoad
 2012-10-15 20:04:26.828 SyneBlogApp[2320:c07] webViewDidFinishLoad

根据日志,我的 webview 完美地加载了数据,但是当我查看模拟器时,我得到了一个白屏.那就是内容没有显示出来...

According to log my webview loads the data perfectly, but when i have a look at the emulator i get a white screen. That is the content is not shown up...

现在只是为了确保 webview 没有问题,我在 DetailViewController viewDiDloadMethod 中尝试了以下工作并显示谷歌页面......

Now just to make sure that nothing is wrong with webview i tried in DetailViewController viewDiDloadMethod the following which works and shows up the google page...

  - (void)viewDidLoad
  {
    self.webView = [[[UIWebView alloc]
                 initWithFrame:CGRectMake(0, 45, self.view.frame.size.width,    self.view.frame.size.height)] autorelease];
    NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

   [self.webView loadRequest:requestObj];
   [self.view addSubview:self.webView];
   [super viewDidLoad];
   NSLog(@"calling viewDidLoad for detailView");
  }

现在我真的不知道出了什么问题.即使在日志中所有事情都运行良好之后,网页内容仍然没有显示出来.. 如果您对出了什么问题有任何线索,请帮助...

Now I seriously have no idea of what is going wrong. Even after all the things are working up well in log still the webpage content is not shown up.. Guys if you have any clue of what is going wrong please help...

推荐答案

这...

   detailViewController = [[DetailViewController alloc]init];

...创建一个新的 DetailViewController 而不是使用您已经创建并在您显示的第一个代码段中作为显示部分的那个.因此,您正在为不在屏幕上的控制器/视图加载 Web 视图.

...creates a new DetailViewController instead of using the one you already created and made part of your display in the first code segment you showed. You are therefore loading up your web view for a controller/view that isn't on screen.

这篇关于UIWebView 在 ios 中不显示内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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