2

現在、NSNotificationCentreを使用して、5つのWebViewのWebViewStartイベントとWebViewFinishイベントを渡します。

WebViewStartメソッド内で、プログレスバーのアニメーションを開始します。WebViewFinishメソッド内で、プログレスバーのアニメーションを停止します。

明らかに問題は、5つのWebViewがロードされていて、1つのWebViewがロードを終了すると、他のWebViewがまだロードされていても、WebViewFinishメソッドを起動してアニメーションを停止することです。

次のようなことを確認する方法はありますか?

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

私が現在持っているコードは、私が持っているWebViewの数に対して適切ではないようです。私が現在使用していて問題があるコードは次のとおりです。

- (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と比較します。それらが同じである場合、そのWebViewはロードを開始/終了しています。

これが誰かに役立つことを願っています。

4

2 に答える 2

0

5つの通知を受け取るまで数えることができます. ビューが一度に 1 つずつ終了するときの進行状況を見積もることもできます。合計の Nx20% をパーセンテージで示します。

于 2015-11-21T11:40:51.590 に答える
0

5 つの Web ビューごとに個別の通知を作成します。各 Web ビューが終了するたびに true に設定できる 5 つのブール値を作成します。Web ビューが終了したら、通知センターに通知を投稿してもらいます。この通知を受け取るメソッドは、最初にそのブール値を true に設定して、終了したことを示す必要があります。次に、5 つのブール値がすべて true に設定されているかどうかを確認します。はいの場合、アクティビティ インジケータを停止します。いいえの場合は、回転させたままにします。

于 2012-04-25T16:50:17.557 に答える