2011-07-01 42 views
0

在我的程序中uiwebview沒有加載url address.when我nslogged請求對象不是null.however當我nslog webview.request它返回null.what可能是它不是原因裝載uiwebview沒有加載網頁

- (void)viewDidLoad { 
     [super viewDidLoad]; 

     self.web = [[UIWebView alloc] init]; 
     NSString *urlAddress = @"http://www.google.com"; 
     NSURL *url = [NSURL URLWithString:urlAddress]; 
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

     NSLog(@"%@",requestObj); 
     [self.web loadRequest:requestObj]; 
     [web setFrame:CGRectMake(0, 0, 320, 370)]; 
     NSLog(@"%@ %@",self.web,self.web.request); 


    } 

的NSLog的結果是

<NSURLRequest http://www.google.com> 
<UIWebView: 0xbb17ff0; frame = (0 0; 320 370); layer = <CALayer: 0xbb12f20>> (null) 

按照從這個網站,我改變它從IB的建議,並使它的代碼。我所做的類確認到UIWebView的delegate..both的webviewdidstart和webview完成被稱爲這些是來自這些方法的nslog輸出

webviewdidstart

webView-----><NSMutableURLRequest > 
webView-----><NSMutableURLRequest http://www.google.com> 

的WebView去把

webView-finished----><NSMutableURLRequest http://www.google.com> 
webView-finished----><NSMutableURLRequest http://www.google.com> 

仍然沒有被調用,我認爲這兩種方法的調用兩次

+1

您的UIWebview是通過IB設置的嗎?如果不是,請首先將webview作爲子視圖添加到您的視圖中。將UIWebviewdelegate添加到您的頭文件並在您的實現文件中設置webViewDidStartLoad:委託。登錄方法檢查您的網頁是否正在加載 –

+0

我正在通過IB設置webview ... – sujith1406

回答

1

嘗試這樣

CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0); 
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame]; 

NSString *urlAddress = @"http://www.google.com"; 
NSURL *url = [NSURL URLWithString:urlAddress]; 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
[webView loadRequest:requestObj]; 
[self addSubview:webView]; 
[webView release]; 
6

這是一個工作的代碼,在應用程序中測試了這一點,讓我們知道結果

在.h文件中

,在.m文件的viewDidLoad添加UIWebViewDelegate

,補充一點:

UIWebView *webview=[[UIWebView alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,460.0)]; 
NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; 
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
webview.delegate = self; 
[webview loadRequest:requestObj]; 
[self.view addSubview:webview]; 

現在檢查代表是否正在加載頁面:

- (void)webViewDidStartLoad:(UIWebView *)webView {  
    NSLog(@"page is loading");  
} 

-(void)webViewDidFinishLoad:(UIWebView *)webView { 
     NSLog(@"finished loading"); 
}