-1

我一直在处理一个需要与 SplitViewController 类似的 GUI 的小项目。现在我所做的是,我有两个 UIViewController,即 MasterViewController 和 DetailViewController,它们被添加为 ViewController 的子视图。 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 ...

MasterTableViewController 类看起来像这样......

   @interface MasterTableViewController : UITableViewController 
   {
     DetailViewController *detailViewController;
   }

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

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

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

 -(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 正常工作..

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

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

 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 完美地加载了数据,但是当我查看模拟器时,我得到一个白屏。那就是内容没有显示出来...

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

  - (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");
  }

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

4

1 回答 1

1

这...

   detailViewController = [[DetailViewController alloc]init];

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

于 2012-10-15T14:52:12.980 回答