2011-02-06 63 views
0

在我的應用我使用的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 
+0

有隻需要重新格式化您的代碼(**請使用所提供的編輯器控件在未來**),我不得不說,我並不感到驚訝你有問題 - 有不少空IFS等奇在那裏建設。 (使用`如果(XYZ!=無)...`是完全合法的,等等) – 2011-02-06 21:59:04

+0

您好我離開IF條件空東陽我不想,如果條件爲真,以處理任何事情。我知道你的意思是如果(xyz!= nil),你的意思是我不能使用if(xyz == nil)?請幫忙! – likki 2011-02-06 22:13:32

回答

1

首先,這裏有一個小底漆平等/不平等的C/Objective-C的:

比方說,你有一個BOOL值(即,可以是YES或NO,「論值'或'關','真'或'假'),稱爲isEnabled。現在,如果我已經分配了此BOOL值(有時稱爲「標誌」)爲「YES」,我可以有條件地測試它的價值,像這樣:

BOOL isEnabled = YES; 

if (isEnabled) 
{ 
    // value set to yes 
} 

除了上述,我可以使用否定運算符(感嘆號 - 另外稱爲NOT運算符)翻轉isEnabled的值,並測試其相反值:在上面的例子isEnabled設置爲YES

BOOL isEnabled = YES; 

// the following reads as "if is *not* enabled" 
if (!isEnabled) 
{ 
    // value set to no 
} 

現在當然,這樣的條件將失敗。但是,如果我們考慮下面的例子,因此如果else if是「滿足」(即真)在一系列if的和else if的任何地方,它會執行所有的代碼裏面,再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的,這是演示代碼是如何工作條件的好方法。

所以在你webViewDidFinishLoad:方法,而不是一個空白,如果來評估的if/else條件,可以替換成一個簡單的條件:

-(void) webViewDidFinishLoad:(UIWebView *)webView 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible=NO; 

    // or, (self.spinner1 != nil) 
    if (!self.spinner1) 
    { 
     [self.spinner1 stopAnimating]; 
    } 
} 

當你作出了上述變化所有你的if,如果是,發佈一個堆棧跟蹤,我會看看還有什麼可能。

相關問題