如何在web加载时停止活动指示符停止?

时间:2015-01-22 22:30:19

标签: ios objective-c uiwebview uiactivityindicatorview

我有一个webview和一个activityIndi​​cator,我希望当URL加载时,activityIndi​​cator会停止。我有这个:

XYZViewController.h

@interface XYZViewController : UIViewController <UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *browser; 

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

@end

XYZViewController.m

-(void)viewDidLoad {
    [super viewDidLoad];

    self.activityIndicator.hidden = NO;
    [self.activityIndicator startAnimating];

    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com"]];

    NSURLRequest* request = [NSURLRequest requestWithURL:url];

    [self.browser loadRequest:request];
}

-(void) webViewDidFinishLoad:(UIWebView *)webView {
    [self.activityIndicator stopAnimating];
    self.activityIndicator.hidden = YES;
} 

AppDelegate.m

XYZViewController* viewVC = [XYZViewController alloc]init];

viewVC.browser.delegate = viewVC;

我不知道这是错的?

有人帮帮我吗?

1 个答案:

答案 0 :(得分:0)

执行@puzzl建议的内容,然后将其添加到webViewDidStartLoad:而不是viewDidLoad

- (void)viewDidLoad {
   ... 
   self.browser.delegate = self
   ...
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    self.activityIndicator.hidden = NO;
    [self.activityIndicator startAnimating];
}