1

我正在调试一个自定义UIViewController子类,它包装了一个UIWebView以便我们可以处理自定义 URL。

我似乎没有问题将相关视图添加到层​​次结构中,但是当我调用loadRequest:webview 时,它什么也不显示。似乎它甚至没有尝试执行请求 - 我的子类将自己设置为UIWebView的委托,但没有从它接收任何委托回调,甚至没有webViewDidStartLoadshouldStartLoadWithRequest(!)。这可能是什么原因造成的?

以下是我班的相关代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    // add the webview
    m_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
    m_webView.delegate = self;
    m_webView.dataDetectorTypes = UIDataDetectorTypeNone;

    // adding a border to see if the view is visible onscreen
    [[m_webView layer] setBorderColor:[[UIColor darkGrayColor] CGColor]];
    [[m_webView layer] setBorderWidth:50];

    [self.view addSubview:m_webView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];

    NSURLRequest *request = [NSURLRequest requestFromUrl:[NSURL urlFromString:@"http://www.google.com"]];
    [self.m_webView loadRequest:request];
    NSLog(@"Loading URL: %@ in view: %@; delegate set as %@", @"http://www.google.com", m_webView, m_webView.delegate);
    NSLog(@"WebView is %@", self.m_webView.loading ? @"loading" : @"not loading");

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"Loading URL of type %@, relative = %@", [[request URL] scheme], [[request URL] relativeString]);
    return YES;
}

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

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"Failed, error %@", error);
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"Finished");
    [self finishedReloading];
}

以及来自控制器的父视图的代码,它试图添加这个实例的一个实例(当前位于该控制器的 viewDidLoad 方法中):

ZUIWebViewController *testWebView = [[[ZUIWebViewController alloc] initWithAddress:@"deal/recentDrops"] autorelease];
[m_scrollView addSubview:testWebView.view];
[testWebView viewWillAppear:YES];
NSLog(@"Added webview");

我运行时的结果是:

Loading URL: http://www.google.com in view: <UIWebView: 0x1114eb30; frame = (0 0; 320 480); layer = <CALayer: 0x1114eb90>>; delegate set as <ZUIWebViewController: 0x1114d7a0>
Webview is not loading
Added webview

“Added webview”消息后的视图层次结构是:

(lldb) po [m_scrollView recursiveDescription]
(id) $1 = 0x0a634b00 <UIView: 0xbba3430; frame = (0 20; 320 460); autoresize = W+H; layer = <CALayer: 0xbba3840>>
   | <UIImageView: 0xbba3490; frame = (0 0; 320 480); userInteractionEnabled = NO; layer = <CALayer: 0xbba3940>>
   | <UIScrollView: 0xbba4390; frame = (0 0; 320 480); clipsToBounds = YES; gestureRecognizers = <NSArray: 0xbba4680>; layer = <CALayer: 0xbba4560>; contentOffset: {0, 0}>
   |    | <UIWebView: 0xbba4840; frame = (0 0; 320 480); layer = <CALayer: 0xbba48a0>>
   |    |    | <_UIWebViewScrollView: 0xbba6b70; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = H; gestureRecognizers = <NSArray: 0xbba6f40>; layer = <CALayer: 0xbba6da0>; contentOffset: {0, 0}>
   |    |    |    | <UIImageView: 0xbba7860; frame = (0 0; 54 54); transform = [-1, 0, -0, -1, 0, 0]; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba78c0>>
   |    |    |    | <UIImageView: 0xbba77d0; frame = (0 0; 54 54); transform = [0, 1, -1, 0, 0, 0]; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba7830>>
   |    |    |    | <UIImageView: 0xbba7740; frame = (0 0; 54 54); transform = [0, -1, 1, 0, 0, 0]; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba77a0>>
   |    |    |    | <UIImageView: 0xbba76b0; frame = (0 0; 54 54); alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba7710>>
   |    |    |    | <UIImageView: 0xbba7620; frame = (-14.5 14.5; 30 1); transform = [0, 1, -1, 0, 0, 0]; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba7680>>
   |    |    |    | <UIImageView: 0xbba7590; frame = (-14.5 14.5; 30 1); transform = [0, -1, 1, 0, 0, 0]; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba75f0>>
   |    |    |    | <UIImageView: 0xbba7500; frame = (0 0; 1 30); transform = [-1, 0, -0, -1, 0, 0]; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba7560>>
   |    |    |    | <UIImageView: 0xbba7470; frame = (0 0; 1 30); alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba74d0>>
   |    |    |    | <UIImageView: 0xbba73e0; frame = (0 450; 320 30); alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba7440>>
   |    |    |    | <UIImageView: 0xbba7380; frame = (0 0; 320 30); transform = [-1, 0, -0, -1, 0, 0]; alpha = 0; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xbba72e0>>
   |    |    |    | <UIWebBrowserView: 0x13baa200; frame = (0 0; 320 480); gestureRecognizers = <NSArray: 0xbba5d50>; layer = <UIWebLayer: 0xbba4930>>
   |    |    |    |    | <TileHostLayer: 0xbba4a00> (layer)
   |    |    |    |    |    | <TileLayer: 0xbba7ef0> (layer)

UIWebView因此,我试图显示的前面似乎没有任何东西。

什么可能导致这种情况,我应该采取任何进一步的步骤来调试吗?

4

1 回答 1

0

您正在 viewWillAppear 中加载 webview,稍后在 viewDidLoad(在 viewWillAppear 之后调用)中您正在创建一个 webview。这应该是其他方式。创建一个 webview,然后加载请求。

只需将所有内容保留在 viewDidLoad 中,否则会出现视图,创建 webview 并加载请求。这应该可以正常工作。

于 2013-04-23T12:01:07.987 回答