检查多个WebView是否已完成加载

时间:2012-04-25 16:42:36

标签: objective-c xcode cocoa

我目前正在使用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已经开始/完成加载。

希望这有助于任何人。

2 个答案:

答案 0 :(得分:0)

为5个Web视图中的每个创建单独的通知。创建5个布尔值,可以在每个Web视图完成时设置为true。 Web视图完成后,让通知中心发布通知。接收此通知的方法应首先将其布尔值设置为true,表示已完成。然后检查是否所有5个布尔值都设置为true。如果是,请停止活动指示器。如果不是,请将其旋转。

答案 1 :(得分:0)

你可以算一下,直到收到5个通知。您也可以估算进度,因为观看次数一次完成一次,总百分比为Nx20%。

相关问题