我正在使用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已經開始/完成加載。
希望這可以幫助任何人。
隨着您的通知註冊,您可以傳遞'nil'作爲'object'參數,並且您將收到發佈該特定通知的所有對象的通知。這樣做會將'applicationDidFinishLaunching:'中的代碼行數減少到兩個而不是十個。 – 2012-04-26 00:18:40