简体   繁体   English

UIWebView未加载页面

[英]UIWebView is not loading a page

I got a UITabBarController and one of the bar items is a Navigation Controller with some buttons on it. 我有一个UITabBarController,其中一个条形项是一个导航控制器,上面有一些按钮。 One of the buttons opens up a urlRequest and load it in a UIWebView. 其中一个按钮打开urlRequest并将其加载到UIWebView中。

NSURL * url = [NSURL URLWithString:myUrl];
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url];
UIWebView * web = [tView wView];
[web setScalesPageToFit:YES];
[web loadHTMLString:@"Loading" baseURL:nil];
[web loadRequest:urlRequest];
[self.navigationController pushViewController:tView animated:YES];

For some reason when i click the button for the first time nothing happens. 出于某种原因,当我第一次点击按钮时没有任何反应。

I used the UIWebViewDelegate protocol to debug it like so: 我使用UIWebViewDelegate协议进行调试,如下所示:

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

}

When i click the button nothing happens, and i don't see the NSLog message. 当我点击按钮没有任何反应,我没有看到NSLog消息。 When i hit back and click the button again, i see the debug and everything works just find. 当我回击并再次单击该按钮时,我看到调试,一切正常。

Any idea what causing this ? 知道是什么导致了这个吗?

PS if i put the : [self.navigationController pushViewController:tView animated:YES]; PS如果我把: [self.navigationController pushViewController:tView animated:YES];

in the webViewDidStarLoad method the application just hang, since it's not loading it on the first click. 在webViewDidStarLoad方法中,应用程序只是挂起,因为它没有在第一次单击时加载它。

You need to make sure that tView's view has been loaded. 您需要确保已加载tView的视图。 When a viewController is instantiated, its view (and all of it's IBOutlets) are all nil, and remain that way until the view is loaded. 当viewController被实例化时,它的视图(以及它的所有IBOutlets)都是nil,并且在加载视图之前保持这种状态。

You have two options: move your load stuff into tView's -viewDidLoad method, or force the view to load before calling [tView wView] by, for example, calling [tView view] (this will force the XIB file to be loaded, and all outlets to be connected. 您有两个选择:将您的加载内容移动到tView的-viewDidLoad方法中,或者在调用[tView wView]之前强制加载视图,例如调用[tView view](这将强制加载XIB文件,以及所有要连接的插座。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM