在我的应用程序中,我使用UIwebview来显示网站,但每当我点击后退按钮并开始在应用程序中的其他部分玩游戏时,该应用程序大多会崩溃,没有任何理由。我怀疑webview导致了这个问题,因为它只在我尝试打开webview时崩溃。
我已经使用NSURLConnection加载webview并制作了webview,在视图中将连接对象设为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
答案 0 :(得分:1)
首先,这是C / Objective-C中关于等式/不等式的小引物:
假设您有一个BOOL
值(即可以是YES或NO,'On'或'Off','True'或'False'的值),称为isEnabled
。现在,如果我将此BOOL
值(有时称为“标记”)指定为“是”,我可以有条件地测试其值,如下所示:
BOOL isEnabled = YES;
if (isEnabled)
{
// value set to yes
}
除了上述内容之外,我还可以使用否定运算符(感叹号 - 也称为NOT运算符)来翻转isEnabled
的值,并测试其相反的值:
BOOL isEnabled = YES;
// the following reads as "if is *not* enabled"
if (!isEnabled)
{
// value set to no
}
现在当然在上面的示例中isEnabled
设置为YES
,因此条件将失败。但是,如果我们考虑以下示例,if
的属性,如果else if
在一系列if
和{{1}中的任何位置“满足”(即,为真)它将执行其中的所有代码,然后忽略其他任何内容:
else if
虽然上面有两个冗余的BOOL isEnabled = NO;
if (isEnabled)
{
// any code here will not run, as the above if condition will be false
}
else if (!isEnabled)
{
// this code will run, since the above condition (!isEnabled) will evaluate to true
}
else if (isEnabled)
{
// this code could never run, since the previous condition was true and all following else if's are ignored
}
else if (!isEnabled)
{
// this code could never run, since the previous condition was true and all following else if's are ignored
}
,但它是演示条件代码如何工作的好方法。
因此,在else if
方法中,您可以使用更简单的条件替换if,而不是空白if if else if条件:
webViewDidFinishLoad:
如果您对所有if和其他if进行了上述更改,请发布堆栈跟踪,我会看到它可能是什么。