开发者

iPhone - UIWebView not loading first request

开发者 https://www.devze.com 2023-02-14 22:23 出处:网络
I have a TableViewController with some items, each item when pressed pushes a ViewController with an UIWebView, the ViewController is the same in all cases, but each item asks the webView to load a di

I have a TableViewController with some items, each item when pressed pushes a ViewController with an UIWebView, the ViewController is the same in all cases, but each item asks the webView to load a diferent URL. Now the problem is that the first time I select a开发者_如何转开发n item, the webView doesn't load the URL, but if I go back in my navigation controller and then press again any of the items (even the same that the first time), it loads correctly. Why could this be?

This is my code. In my TableViewController I have this inside the didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     
 NSString *urlAddress;
 NSInteger row = [indexPath row];

 // SELECT THE URL BASED ON THE SELECTED ROW. SECTION OMMITED FOR CLARITY PURPOSES.
    urlAddress = @"http://www.google.com";

 [browserViewController refreshUrl:urlAddress];
 [urlAddress release];

 browserViewController.title = [NSString stringWithFormat:@"%@", [itemsArray objectAtIndex:row]];

 MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
 [delegate.mainNavController pushViewController:browserViewController animated:YES]; }

The browserViewController is initialized in the viewDidLoad method of the TableViewController. In the BrowserViewController implementation I have this method:

- (void) refreshUrl: (NSString *)theUrlAddress{
 NSURL *url = [NSURL URLWithString: theUrlAddress];
 NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
 [webView loadRequest:requestObj];}

But this is the method that seems to be working only from the second time is invoked. If I set a breakpoint on it, I can see it is actually being invoked the first time I press the item on the table, but the loadRequest does nothing.

BrowserViewController is also an UIWebViewDelegate. The webViewDidStartLoad and webViewDidFinishLoad methods aren't being invoked the first time either.

Any ideas of why and how to fix it? Thanks!


you are using refreshUrl on the viewController before it has created its UIWebView.
Use a breakpoint and check the value of your webview. I think it is nil for the first time.

Simply move [browserViewController refreshUrl:urlAddress]; to the line after the push.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号