UIActivityIndi​​catorView无法在mySubView中以编程方式运行UIWebView

时间:2011-05-23 07:34:14

标签: iphone uiwebview uiactivityindicatorview

我的myViewController.h包含:

#import <UIKit/UIKit.h>

@interface myViewController : UIViewController {

    UIScrollView *myScrollView;

    UIView *mySubView;

    UIWebView *myWebView;

    UIActivityIndicatorView *myIndicator;

}

@property (nonatomic, retain) IBOutlet UIScrollView *myScrollView;

@end

我的myViewController.m包含:

#import "myViewController.h"

@implementation myViewController

@synthesize myScrollView;

...

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *bgColors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];

    for (int i = 0; i < bgColors.count; i++)
    {
        CGRect frameNew;
        frameNew.origin.x = self.myScrollView.frame.size.width * i;
        frameNew.origin.y = 0;
        frameNew.size = self.myScrollView.frame.size;

        mySubView = [[UIView alloc] initWithFrame:frameNew];
        mySubView.backgroundColor = [bgColors objectAtIndex:i];

        [self.myScrollView addSubview:mySubView];

        myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        myIndicator.center = CGPointMake(160, 100);
        myIndicator.hidesWhenStopped = NO;

        CGRect webFrame = CGRectMake(self.myScrollView.frame.size.width * i, 0, 320, 320);
        myWebView = [[UIWebView alloc] initWithFrame:webFrame];
        [myWebView setDelegate:(id)self];
        [myWebView addSubview:myIndicator];

        NSString *urlAddress = @"http://www.google.com/";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
        [myWebView loadRequest:requestObj];

        [self.myScrollView addSubview:myWebView];

        [myIndicator release];
        [myWebView release];
        [mySubView release];
    }

    self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * bgColors.count, self.myScrollView.frame.size.height);
}

-(void)webViewDidStartLoad: (UIWebView *)webView
{
    NSLog(@"Web view did start loading");
    [myIndicator startAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

-(void)webViewDidFinishLoad: (UIWebView *)webView
{
    NSLog(@"Web view did finish loading");
    [myIndicator stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [myIndicator stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSString* errorString = [NSString stringWithFormat:@"<html>%@</html>", error.localizedDescription];
    [myWebView loadHTMLString:errorString baseURL:nil];
}

...

加载指示符不适用于mySubView中的前两个myWebView,仅适用于mySubView中的最后一个myWebView

mySubView中的前两个myWebView中,myIndicator显示但未激活。

告诉我做错了什么?

1 个答案:

答案 0 :(得分:4)

这是因为myIndicator指向最后一个活动指示器视图。之前的所有参考文献都丢失幸运的是,由于它是Web视图的子视图,我们可以参考它。这应该有帮助 -

-(void)webViewDidStartLoad: (UIWebView *)webView
{
    UIActivityIndicatorView *actView;
    for ( id object in webView.subviews ) {
        if ([object isMemberOfClass:[UIActivityIndicatorView class]]) {
            actView = (UIActivityIndicatorView*)object;
        }
    }
    NSLog(@"Web view did start loading");
    [actView startAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
}

-(void)webViewDidFinishLoad: (UIWebView *)webView
{
    UIActivityIndicatorView *actView;
    for ( id object in webView.subviews ) {
        if ([object isMemberOfClass:[UIActivityIndicatorView class]]) {
            actView = (UIActivityIndicatorView*)object;
        }
    }
    NSLog(@"Web view did finish loading");
    [actView stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    UIActivityIndicatorView *actView;
    for ( id object in webView.subviews ) {
        if ([object isMemberOfClass:[UIActivityIndicatorView class]]) {
            actView = (UIActivityIndicatorView*)object;
        }
    }
    [actView stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSString* errorString = [NSString stringWithFormat:@"<html>%@</html>", error.localizedDescription];
    [myWebView loadHTMLString:errorString baseURL:nil];
}