2011-03-17 91 views
0

我正在寫一個iPhone應用程序,並且有加載,看起來像這樣的URL一個UIWebView:更改URL並重新加載UIWebView?

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

網站知道使用query=後什麼來作爲網站上的搜索詞。在這種情況下,「狗」。

應用程序中的WebView上方有一個搜索欄,用戶可以使用該搜索欄搜索其他內容。因此,如果用戶鍵入「貓」到搜索欄和點擊搜索,我要的網址改成這樣:

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

我不能讓它工作,雖然。它不斷加載頁面,就像我在尋找狗。任何人都可以啓發我如何更改URL並刷新WebView?以下是我的代碼的樣子:

- (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]; 
} 

回答

3

刪除呼叫reload。這完全不需要,可能是你麻煩的原因。

我在想,新URL的請求開始加載,並且在它完成之前刷新已加載的URL來取消加載。這就像點擊瀏覽器中的鏈接,然後在加載新頁面之前按下刷新按鈕。你最終會刷新你以前的頁面。

+0

你說得對您的網址。這對我來說很愚蠢。謝謝。 – rottendevice 2011-03-17 17:47:49

2

正確撰寫您的網址字符串。

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

取下重裝,並創建正確使用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]; 
}