UIActivityIndi​​catorView 无法以编程方式在 mySubView 中的 UIWebView 上工作

发布于 2024-11-09 06:53:58 字数 3093 浏览 0 评论 0原文

我的 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];
}

...

加载指示器在 myWebView 中的前两个 mySubView 上不起作用,仅在最后一个 mySubView 上起作用myWebView 中的代码>。

myWebView 中的前两个 mySubView 中,myIndi​​cator 显示但未激活。

告诉我做错了什么?

My myViewController.h contains:

#import <UIKit/UIKit.h>

@interface myViewController : UIViewController {

    UIScrollView *myScrollView;

    UIView *mySubView;

    UIWebView *myWebView;

    UIActivityIndicatorView *myIndicator;

}

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

@end

My myViewController.m contains:

#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];
}

...

Load indicator does not work on the first two mySubView in myWebView, works only on the last mySubView in myWebView.

In the first two mySubView in myWebView, myIndicator displayed but not active.

Tell me what's wrong doing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与酒说心事 2024-11-16 06:53:58

这是因为 myIndi​​cator 指向最后一个活动指示器视图。所有以前的参考文献都丢失了。幸运的是,由于它是 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];
}

That is because myIndicator is pointing to the last activity indicator view. All previous references are lost. Luckily, since it is a subview of the web view, we can get a reference to it. This should help –

-(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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文