阻止webViewDidFinishLoad被快速调用

时间:2012-07-12 11:41:18

标签: iphone objective-c cocoa-touch sdk

在我的webViewDidFinishLoad方法中,我有以下代码:

- (void)webViewDidFinishLoad:(UIWebView *)webView {   
    if (hasPressed == 1) {
        hasPressed = 0;
        isBlocked = 0;
    }
    else if (hasPressed == 0 && hasDroppedDown == 0) {
        if (viewState != kStateWeather || isBlocked == 0) {
        [UIView animateWithDuration:0.5 
                              delay:0.0 options:UIViewAnimationCurveLinear animations:^{
                                  //Animations
                              }
                         completion:^(BOOL finished){
                             hasDroppedDown = 1;
                         }];
        }
    }
}

如您所见,如果快速调用此方法,例如一个具有重定向的网站,我else中的else if变得毫无用处。如何阻止它被如此迅速地召唤?

2 个答案:

答案 0 :(得分:1)

不幸的是,你不能这样做。当UIWebView开始加载新网址时,它会调用。相反,你可以做一件事。 实现委托功能

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

当新框架即将开始加载UIWebView时,将调用此委托。 您可以通过选中可以获得值的navigationType

来确定要加载哪种类型的请求
UIWebViewNavigationTypeLinkClicked
UIWebViewNavigationTypeFormSubmitted
UIWebViewNavigationTypeBackForward
UIWebViewNavigationTypeReload
UIWebViewNavigationTypeFormResubmitted
UIWebViewNavigationTypeOther

注意:如果您允许webview加载其他网页YES,请返回NO

希望这对你有所帮助。

答案 1 :(得分:1)

- (void)main { //being called every 0.2
    t+=0.2;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {   
    loadImg.hidden = 1;
    m_activity.hidden= TRUE;     
    [m_activity stopAnimating];
    if (t>0.5) {
        [self finishedLoading];
        t=0;
    }
}
- (void)finishedLoading {
    if (hasPressed == 1) {
        hasPressed = 0;
        isBlocked = 0;
    }
    else if (hasPressed == 0 && hasDroppedDown == 0) {
        if (viewState != kStateWeather || isBlocked == 0) {
            [UIView animateWithDuration:0.5 
                                  delay:0.0 options:UIViewAnimationCurveLinear animations:^{
                                      //Animations
                                  }
                             completion:^(BOOL finished){
                                 hasDroppedDown = 1;
                             }];
        }
    }
}