在我的應用我使用的UIWebView顯示網頁,但是每當我單擊後退按鈕並開始與應用程式中其他部分播放,應用程序大多是沒有理由崩潰。我懷疑webview導致這個問題,因爲它只有當我嘗試打開webview時崩潰。應用程序崩潰是由於的WebView
我已經使用NSURLConnection的加載網頁視圖,並取得了web視圖,連接對象爲nil在視圖中會消失方法。
@implementation NewsWebSiteViewController
@synthesize connection,rcvdData,spinner1,currentSite,webView,newsWebsite;
- (void)viewDidLoad {
[super viewDidLoad];
@try {
self.webView.delegate=self;
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
self.title= self.newsWebsite.title;
self.webView.backgroundColor =[UIColor groupTableViewBackgroundColor];
self.spinner1 = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
CGRect center = [self.view bounds];
CGSize winCenter = center.size;
CGPoint pont = CGPointMake(winCenter.width/2,winCenter.height/2);
[spinner1 setCenter:pont];
[self.view addSubview:spinner1];
[self.spinner1 startAnimating];
NSString *url = newsWebsite.link;
NSURLRequest *theReq =[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
self.connection = [[NSURLConnection alloc] initWithRequest:theReq delegate:self];
if(self.connection) {
self.rcvdData = [[NSMutableData data] retain];
}
else {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error!" message:@"We are having a problem connecting to the internet, why not try again or try sometime later!.." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
webView.multipleTouchEnabled=YES;
webView.scalesPageToFit=YES;
}
@catch (NSException * e) {
}
}
-(void) goBack {
self.webView =nil;
[self.navigationController popViewControllerAnimated:YES];
}
-(void) viewWillDisappear:(BOOL)animated {
[self.connection cancel];
self.connection=nil;
[self.webView stopLoading];
self.webView=nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
if (self.spinner1 ==nil) {
}
else {
[self.spinner1 stopAnimating];
}
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
if (self.spinner1 ==nil) {
}
else {
[self.spinner1 stopAnimating];
}
}
-(void) viewDidAppear:(BOOL)animated {
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.rcvdData setLength:0];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.rcvdData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (self.spinner1 ==nil) {
}
else {
[self.spinner1 stopAnimating];
}
[connection release];
[rcvdData release];
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error!" message:@"We are having a problem connecting to the internet, why not try again or try sometime later!.." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
[rcvdData release];
NSString *url = newsWebsite.link;
NSURL *url1 = [NSURL URLWithString:url];
[self.webView loadData:self.rcvdData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:url1];
}
- (void)dealloc {
[super dealloc];
[self.spinner1 release];
}
@end
有隻需要重新格式化您的代碼(**請使用所提供的編輯器控件在未來**),我不得不說,我並不感到驚訝你有問題 - 有不少空IFS等奇在那裏建設。 (使用`如果(XYZ!=無)...`是完全合法的,等等) – 2011-02-06 21:59:04
您好我離開IF條件空東陽我不想,如果條件爲真,以處理任何事情。我知道你的意思是如果(xyz!= nil),你的意思是我不能使用if(xyz == nil)?請幫忙! – likki 2011-02-06 22:13:32