如何在加载URL期间使用progressView?

时间:2014-11-18 20:31:32

标签: ios xcode url uiwebview uiprogressview

我想在我的网址加载时使用progressView。我该怎么办?

这是我的.m代码:

-(void)openURL{
UIWebView *webView = [[UIWebView alloc] init];
[webView setFrame:CGRectMake(0, 0, 320, 460)];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];
[[self view] addSubview:webView];
}

- (IBAction)goto:(id)sender {


[self openURL];
}

2 个答案:

答案 0 :(得分:1)

实施UIWebViewDelegate,具体为:

-(void)openURL{
    UIWebView *webView = [[UIWebView alloc] init];
    webView.delegate = self;
    // ... rest of method as above ...
}

- webViewDidStartLoad:(UIWebView *)webView {
    // Start progress.
}

- webViewDidFinishLoad:(UIWebView *)webView {
    // Stop progress.
}

答案 1 :(得分:0)

你可以做这样的事情

[progressView startAnimating];

dispatch_async(dispatch_get_main_queue(), ^{
    [webView loadRequest:yourRequest];

    [progressView stopAnimating];
    [progressView removeFromSuperView];
}

这将在操作开始之前启动进度视图,并在完成时停止并将其删除。