2012-04-25 9 views
2

現在、NSNotificationCentreを使用して5つのWebViewのWebViewStartイベントとWebViewFinishイベントを渡しています。複数のWebViewが終了したことを確認します。

WebViewStartメソッドの中で、私はプログレスバーのアニメーションを開始します。 WebViewFinishメソッドの中で、私は進行状況バーのアニメーションを停止します。

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

次のような方法はありますか?

- (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]; 
} 

私は、誰かが助けることができると思います。事前にみんなありがとう!

EDIT:私は解決策を自分で見つけることになりました。ではないかもしれない最もエレガントな、しかしそれにもかかわらず:

- (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で通知オブジェクトを、取得し、私たちのWebViewsと比較します。それらが同じ場合、そのWebViewはロードを開始/終了しました。

が、これは誰にもお役に立てば幸いです。

+0

通知登録では、 'object'パラメータとして' nil'を渡すことができ、その特定の通知を送信するすべてのオブジェクトからの通知を受け取ります。そうすることで 'applicationDidFinishLaunching:'のコード行が10行ではなく2行に減らされます。 –

答えて

0

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

+0

私はこれに似た何かを考えていました。このメソッドで問題が発生するのは簡単ですが、メソッド 'WebViewFinishProgress'からWebViewの名前を取得する方法がわかりません。どのWebViewが読み込みを完了したかをどのように知ることができますか? Answer – Cristian

+0

webViewDidFinishLoad:メソッド内で簡単な比較を試してみてください。デリゲートメソッドは、参照のために呼び出すUIWebViewと共に提供されています。他の名前に上書きしない限り、デフォルトで "webView"として渡されます。したがって、あなただけのようにテストすることができるはずです: 'if(webView = myOtherWebViewObject){このコードを実行;}' – Kyle

+0

これはiOS用ではありません。私はどのように比較するつもりですか?もう一度ありがとう – Cristian

0

5つの通知が届くまでカウントすることができます。ビューが一度に1つずつ終了するにつれて進行状況を見積もることもできます。集計のNx20%はパーセンテージで終了します。

関連する問題