开发者

Changing a URL and reloading the UIWebView?

I'm writing an iPhone app, and there's a UIWebView that loads a URL that looks like this:

http://www.example.com/search?query=dog

The website knows to use whatever comes after query= as a search term on the site. In this case, "dog".

There开发者_运维问答's a search bar above the WebView in the app which the user can use to search for something else. So if the user were to type "cat" into the search bar and hit search, I want the URL to change to this:

http://www.example.com/search?query=cat

I can't get it to work though. It keeps loading up the page as if I were searching for dog. Can anyone enlighten me on how to change the URL and refresh the WebView? Here's what my code looks like:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    urlString = @"http://www.example.com/search?query=%i", searchBar.text;
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    [self.myWebView reload];
    [searchBar setShowsCancelButton:NO animated:YES];
}   


Remove the call to reload. It's totally not needed, and likely the cause of your trouble.

I'm thinking that the request for the new URL starts loading, and before it's done it refreshes the already loaded URL canceling the load. It's like clicking a link in your browser, then pressing the refresh button before the new page loads. You just end up refreshing the page you were on before.


Compose your URL String correctly.

 urlString =[[NSStrng alloc] initWithFormat:@"http://www.example.com/search?query=%@", searchBar.text];


Remove the reload and create your url correctly using stringWithFormat:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    urlString = [NSString stringWithFormat:@"http://www.example.com/search?query=%@", searchBar.text];
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
    [searchBar setShowsCancelButton:NO animated:YES];
}  
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜