UIActivityIndicatorView 无法以编程方式在 mySubView 中的 UIWebView 上工作
我的 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
中,myIndicator
显示但未激活。
告诉我做错了什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为
myIndicator
指向最后一个活动指示器视图。所有以前的参考文献都丢失了。幸运的是,由于它是 Web 视图的子视图,因此我们可以获得对它的引用。这应该有帮助——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 –