我正在调试一个自定义UIViewController
子类,它包装UIWebView
,以便我们可以处理自定义网址。
我似乎没有问题将相关视图添加到层次结构中,但是当我在webview上调用loadRequest:
时,它什么也没显示。它似乎好像它甚至都没有尝试执行请求 - 我的子类将自己设置为UIWebView
的委托,但是没有从它接收任何委托回调,而不是偶数webViewDidStartLoad
或shouldStartLoadWithRequest
(!)。可能导致这种情况的原因是什么?
以下是我班级的相关代码:
- (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
“添加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
前面似乎没有任何内容。
可能导致这种情况的原因是什么?我应该采取进一步调试的步骤吗?
答案 0 :(得分:0)
您正在viewWillAppear中加载webview,稍后在viewDidLoad(在viewWillAppear之后调用)中加载webview。这应该是另一种方式。创建一个webview,然后加载请求。
只需保留viewDidLoad或视图中的所有内容,创建webview和加载请求即可。这应该工作正常。