2011-08-31 28 views
0

我創建了下面的代碼來檢查頁面加載時是否有互聯網連接。如果沒有;會彈出一條消息。如果有的話,什麼都不會彈出。 我遇到的問題是,當有一個互聯網連接和UIWebView加載,同時我按下其他按鈕,如後退或前進,沒有互聯網連接的消息彈出!任何想法我怎麼能避免這一點?有沒有辦法禁用其他行動,而我的UIWebView加載?如何在UIWebView加載時禁用動作?

代碼:

- (void)webView:(UIWebView *)NBC didFailLoadWithError:(NSError *)error 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" 
         message:@"Please verify your connection" 
         delegate:self cancelButtonTitle:@"Dismiss" 
         otherButtonTitles:/*@"Verify", */nil]; 
    [alert show]; 
    [alert release]; 
} 

非常感謝您的幫助!

回答

2
-(void)webViewDidStartLoad:(UIWebView *)webView { 

    [buttonForward setUserInteractionEnabled:NO]; 

} 


- (void)webViewDidFinishLoad:(UIWebView *)webView { 

    [buttonForward setUserInteractionEnabled:YES]; 

} 
+0

感謝您的回答。比方說,IBAction爲對前進按鈕是: - (IBAction爲)GoForward:(ID)發送{ [TheWebView goForward] } 產生的代碼應該是: -(void)webViewDidStartLoad:(UIWebView *)TheWebView{ \t [GoForward setUserInteractionEnabled:NO]; } 雖然我得到這個錯誤:GoForward undecleared。 任何想法? – Andres

+0

好的,是的,你必須在.h文件中定義你的按鈕,並將它們鏈接到XIB文件中的按鈕,然後用前進按鈕名稱替換GoForward,然後複製該行並添加返回按鈕的名稱。另外,如果我的答案是正確的,你能否將其標記爲這樣?謝謝! –

+0

我在.h中定義它爲:@ property(nonatomic,retain)IBOutlet UIButton * GoForward;然後合成它:@synthesize GoForward;它編譯時不會給我任何錯誤,但仍然存在與以前相同的問題(我發佈的問題) – Andres

0

我不是封鎖用戶界面的粉絲,除非它很關鍵。爲什麼不檢查正確的錯誤類型,然後決定向用戶顯示警報?

我敢打賭,點擊另一件事是使UIWebView中止NSErrorNSURLErrorCancelled,這是觸發您的警報代碼。

Here's a list of the foundation errors

+0

嗨,其實我沒有阻止任何用戶界面。我很新的iOS編程,但我的意圖是,如果我不得不用C#語言說它會是: if (webBrowse.IsBusy) {btnForward.Enabled = false; btnRefresh.Enabled = true; } else {btnRefresh.Enabled = true; } btnStop.Enabled = false;} 這就是我想得到的... – Andres

1

使用此代碼,

[self.webView setUserInteractionEnabled:NO]; 

這可能會幫助你.....