2

我想将活动指示器添加到 Web 视图。但我不知道 web 视图何时完成加载。我开始在 viewdidload 中制作动画..

4

4 回答 4

19

您不应该在 viewDidLoad 中开始制作动画。符合

UIWebViewDelegate

协议并使您的 Web 视图的委托成为您的视图控制器,然后使用委托方法:

@interface MyVC: UIViewController <UIWebViewDelegate> {
    UIWebView *webView;
    UIActivityIndicatorView *activityIndicator;
}

@end

@implementation MyVC

- (id)init
{
    self = [super init];
    // ...

    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityIndicator.frame = CGRectMake(x, y, w, h);
    [self.view addSubview:activityIndicator];

    webView = [[UIWebView alloc] initWithFrame:CGRectMake(x, y, w, h)];
    webView.delegate = self;
    // ...
    return self;
}

- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq
{
    [activityIndicator startAnimating];
    return YES;
}

- (void)webViewDidFinishLoading:(UIWebView *)wv
{
    [activityIndicator stopAnimating];
}

- (void)webView:(UIWebView *)wv didFailLoadWithError:(NSError *)error
{
    [activityIndicator stopAnimating];
}

@end
于 2012-07-04T19:08:19.573 回答
5

实现UIWebViewDelegate协议 这些是您需要在代码中实现的委托:

- (void)webViewDidStartLoad:(UIWebView *)webView; //a web view starts loading
- (void)webViewDidFinishLoad:(UIWebView *)webView;//web view finishes loading
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error; //web view failed to load
于 2012-07-04T19:07:11.913 回答
2

您将需要侦听 Web 视图委托回调以正确显示您的活动指示器。

具体来说,您将要收听:

webViewDidStartLoad:(开始你的活动指示器动画)

webViewDidFinishLoad:(结束)

webView:didFailLoadWithError: (结束)

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html

于 2012-07-04T19:02:19.843 回答
0
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

self.webViewRef.delegate = self;
NSURL *websiteUrl = [NSURL URLWithString:Constants.API_TERMS_AND_CONDITIONS_URL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[self.webViewRef loadRequest:urlRequest];
}

#pragma mark
#pragma  mark -- UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
[self.activityIndicator startAnimating];
return YES;
}
 - (void)webViewDidStartLoad:(UIWebView *)webView{
[self.activityIndicator startAnimating];
 }
  - (void)webViewDidFinishLoad:(UIWebView *)webView{
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
 }
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
[self.activityIndicator stopAnimating];
self.activityIndicator.hidden = YES;
 }
于 2016-04-22T06:56:52.083 回答