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

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

问题描述:

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

希望这可以帮助任何人。

+0

随着您的通知注册,您可以传递'nil'作为'object'参数,并且您将收到发布该特定通知的所有对象的通知。这样做会将'applicationDidFinishLaunching:'中的代码行数减少到两个而不是十个。 – 2012-04-26 00:18:40

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

+0

我在想这件事情。我用这个方法的问题很简单,我不知道如何从方法'WebViewFinishProgress'中检索WebView的名称,那么如何知道哪个WebView已经完成加载?感谢您的回答 – Cristian 2012-04-25 16:53:01

+0

尝试在webViewDidFinishLoad:方法内做一个简单的比较。委托方法随调用UIWebView提供以供参考。它被默认作为“webView”传入,除非您已将其重命名为其他名称。因此,你应该能够像这样测试:'if(webView = myOtherWebViewObject){执行此代码;}' – Kyle 2012-04-25 16:58:19

+0

只是为了澄清这是不适用于iOS它是Mac。我将如何去比较?再次感谢 – Cristian 2012-04-25 16:59:32

你可以直到你得到5个通知。您还可以根据观看次数完成一次评估进度,Nx20%为百分比总计。