我目前正在使用 NSNotificationCentre 为 5 个 WebView 传递 WebViewStart 和 WebViewFinish 事件。
在 WebViewStart 方法中,我启动了进度条的动画。在 WebViewFinish 方法中,我停止了进度条的动画。
显然问题在于,如果正在加载 5 个 WebView,并且一个 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];
}
我希望有人能帮帮忙。提前谢谢大家!
编辑:我最终自己找到了解决方案。可能不是最优雅的,但尽管如此:
- (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 已开始/完成加载。
希望这对任何人都有帮助。