简体   繁体   中英

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. One of the buttons opens up a urlRequest and load it in a 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:

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

}

When i click the button nothing happens, and i don't see the NSLog message. 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];

in the webViewDidStarLoad method the application just hang, since it's not loading it on the first click.

You need to make sure that tView's view has been loaded. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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