2011-11-30 106 views
0

我需要從UIWebView中獲取一個URL並將其放入地址欄。我使用這個代碼,但它根本不起作用。將webview網址放在地址欄中

- (void)webViewDidStartLoad:(UIWebView *)webView2 shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{ 
    NSURL *URL = [request URL]; 
    [webAdress2 setText:[URL absoluteString]]; 
} 

請幫忙!

+0

你的代碼似乎對我來說,錯誤必須在別處。 –

+0

你需要提供更多關於webAdress2的信息,它是什麼,你在哪裏創建它等 – jbat100

+0

我設置webAdress2和webView2像這樣 - NSString * settingValue2 = [[NSUserDefaults standardUserDefaults] stringForKey:@「url2」]; NSURL * urlx2 = [NSURL URLWithString:[NSString stringWithFormat:@「%@」,settingValue2]]; NSURLRequest * request2 = [NSURLRequest requestWithURL:urlx2]; [webView2 loadRequest:request2]; [webAdress2 setText:settingValue2]; – nik1004

回答

1

今天我有類似的問題。很明顯,[請求URL]在webView加載完成後纔會獲得值。所以我做了一個控制器中的屬性與網頁視圖,這樣我可以爲它從中調用控制器,並像當年剛用過的東西類設置的值:

[webAdress2 setText:self.theURL]; 

希望它可以幫助

+0

你可以給我這個想法的源代碼嗎? – nik1004

+0

基本上在調用帶有UIWebView的ViewController之前,調用一個方法如 someViewController.theURL = theURLYouNeed; 它將使該viewController的URL與theURLYouNeed相同。 – Novarg