2011-07-01 74 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"); 
}