2012-04-25 124 views
2

我正在使用NSNotificationCentre爲5個WebViews傳遞WebViewStart和WebViewFinish事件。檢查多個WebViews是否已完成加載

在WebViewStart方法中,我開始進度條的動畫。 在WebViewFinish方法中,我停止了進度條的動畫。

很明顯,問題是如果加載了5個WebViews,並且一個WebView完成加載,它將觸發WebViewFinish方法並停止動畫,即使其他WebViews仍在加載。

有沒有什麼辦法可以檢查如下內容?

- (void)_webViewProgressFinished:(NSNotification *)notification 
{ 
    if ([webView1 & webView2 & webView3 finishedLoading]) { 
     [_loadingIndicator stopAnimation:self]; 
    } 
} 

我現在的代碼似乎並不適合我擁有的WebViews的數量。我目前使用的代碼有問題如下:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_mainWebView]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_mainWebView]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView1]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView1]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView2]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView2]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView3]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView3]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView4]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView4]; 
} 

- (void)_webViewProgressStarted:(NSNotification *)notification 
{ 
    [_loadingIndicator startAnimation:self]; 
} 

- (void)_webViewProgressFinished:(NSNotification *)notification 
{ 
    [_loadingIndicator stopAnimation:self]; 
} 

我希望有人能幫忙。在此先感謝大家!

編輯:我最終找到了自己的解決方案。未必是最優雅的,但儘管如此:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_mainWebView]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_mainWebView]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView1]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView1]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView2]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView2]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView3]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView3]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView4]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView4]; 
} 

- (void)_webViewProgressStarted:(NSNotification *)notification 
{ 
    if ([_mainWebView isEqual:[notification object]]) { 
     [_mainWebViewProgress startAnimation:self]; 
    } else if ([_subWebView1 isEqual:[notification object]]) { 
     [_subView1Progress startAnimation:self];  
    } else if ([_subWebView2 isEqual:[notification object]]) { 
    [_subView2Progress startAnimation:self]; 
    } else if ([_subWebView3 isEqual:[notification object]]) { 
     [_subView3Progress startAnimation:self]; 
    } else if ([_subWebView4 isEqual:[notification object]]) { 
     [_subView4Progress startAnimation:self]; 
    } 
} 

這樣做是得到通知的對象,這是一個ID,並將它與我們的網頁視圖。如果它們相同,那麼WebView已經開始/完成加載。

希望這可以幫助任何人。

+0

隨着您的通知註冊,您可以傳遞'nil'作爲'object'參數,並且您將收到發佈該特定通知的所有對象的通知。這樣做會將'applicationDidFinishLaunching:'中的代碼行數減少到兩個而不是十個。 – 2012-04-26 00:18:40

回答

0

爲5個Web視圖中的每一個創建單獨的通知。創建5個布爾值,可在每個Web視圖完成時將其設置爲true。當網絡視圖結束時,讓通知中心發佈通知。接收此通知的方法應該首先將其布爾值設置爲true,表示它已完成。然後檢查所有5個布爾值是否都設置爲true。如果是,請停止活動指示器。如果不是,則讓它旋轉。

+0

我在想這件事情。我用這個方法的問題很簡單,我不知道如何從方法'WebViewFinishProgress'中檢索WebView的名稱,那麼如何知道哪個WebView已經完成加載?感謝您的回答 – Cristian 2012-04-25 16:53:01

+0

嘗試在webViewDidFinishLoad:方法內做一個簡單的比較。委託方法隨調用UIWebView提供以供參考。它被默認作爲「webView」傳入,除非您已將其重命名爲其他名稱。因此,你應該能夠像這樣測試:'if(webView = myOtherWebViewObject){執行此代碼;}' – Kyle 2012-04-25 16:58:19

+0

只是爲了澄清這是不適用於iOS它是Mac。我將如何去比較?再次感謝 – Cristian 2012-04-25 16:59:32

0

你可以直到你得到5個通知。您還可以根據觀看次數完成一次評估進度,Nx20%爲百分比總計。