現在、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はロードを開始/終了しました。
が、これは誰にもお役に立てば幸いです。
通知登録では、 'object'パラメータとして' nil'を渡すことができ、その特定の通知を送信するすべてのオブジェクトからの通知を受け取ります。そうすることで 'applicationDidFinishLaunching:'のコード行が10行ではなく2行に減らされます。 –