【发布时间】:2011-02-06 21:52:36
【问题描述】:
在我的应用程序中,我使用 UIwebview 来显示网站,但是每当我单击后退按钮并开始使用应用程序中的其他部分时,应用程序大多会无缘无故地崩溃。我怀疑是 webview 导致了这个问题,因为它只有在我尝试打开 webview 时才会崩溃。
我已经使用NSURLConnection来加载webview并制作了webview,视图中为nil的连接对象将Disappear方法。
@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
【问题讨论】:
-
只需要重新格式化您的代码(请在以后使用提供的编辑器控件)我不得不说我对您遇到问题并不感到惊讶 - 有那里有很多空的 if 和其他奇怪的结构。 (使用
if(xyz != nil)...是完全合法的,等等) -
嗨,我将 IF 条件留空,因为如果条件为真,我不想处理任何内容。我不明白你所说的 if(xyz!=nil) 是什么意思,你的意思是我不能使用 if(xyz==nil) 吗?请帮忙!
-
在 C 风格的语言中,
!=检查 inequality。如,仅当条件 not 满足时才返回 true。这基本上可以让您编写更整洁的代码。我建议你把所有这些代码都扔掉,然后再试一次。或者,您可以向我们展示您的崩溃的堆栈跟踪。 -
嗨,我基本上是应用程序开发的新手,你能告诉我如何查看堆栈跟踪吗?