同步方法我看到有同步的方法,就像如果我想要做這樣的事情:異步VS iPhone上
-(IBAction)doNSURLConnASync {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
if (connection) {
responseData = [[NSMutableData alloc] init];
[webview loadHTMLString:@"" baseURL:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Network error occured"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"%s", __FUNCTION__);
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[connection release]; // got passed in as a param. we are done with it now.
[webview loadData:responseData
MIMEType:nil
textEncodingName:nil
baseURL:nil];
[responseData release];
}
:
-(IBAction)doNSURLConnSync {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error = nil;
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
}
它是如何比我做異步執行不同
另外,與我的doNSURLConnSync方法,我只是試圖加載一個UIWebView。有沒有理由不這樣做?該按鈕剛好位於此處並保持突出顯示狀態,但它嘗試訪問網頁,但與異步版本相比,最終沒有任何結果。
此外,對於我的異步代碼中的networkactivityindicator,我想將我的UIWebView設置爲空白,當我的webview加載時使指示器亮起,然後在頁面加載後關閉網絡活動指示器。但是,如果我刪除了loadHTMLString方法,則網絡活動指示器按其應有的方式工作,但對於loadHTMLString,UIWebView將變爲空白,但網絡活動指示器不會。有什麼想法嗎?謝謝。
這是一類任務只是加載實際之前做出的WebView空白因爲我們以幾種不同的方式訪問了網頁,並且應該顯示不同的方法。 – Crystal 2010-07-29 16:21:51