我想知道如何在基于WebView的应用程序中实现activityIndicator,我写了下面的代码,但指标没有出现.
webview在本地加载文件,因此加载速度非常快,但是当它加载外部页面时加载速度很慢,我需要指标...
FirstViewController.h
#import@interface FirstViewController : UIViewController { IBOutlet UIWebView *webview1; NSURL *urlLocation; IBOutlet UIActivityIndicatorView *m_activity; } @property (nonatomic, retain) UIActivityIndicatorView *m_activity; - (IBAction)searchbutton:(id)sender; - (IBAction)home:(id)sender; @end
FirstViewController.m
#import "FirstViewController.h" @implementation FirstViewController @synthesize m_activity; // viewWillAppear loads every time younopen up this View - (void)viewWillAppear:(BOOL)animated { NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; urlLocation = [NSURL fileURLWithPath:filePath]; [webview1 loadRequest:[NSURLRequest requestWithURL:urlLocation]]; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { //Initialization code m_activity = nil; } return self; } - (void)webViewDidFinishLoad:(UIWebView *)webView { m_activity.hidden= TRUE; [m_activity stopAnimating]; NSLog(@"Web View started loading..."); } - (void)webViewDidStartLoad:(UIWebView *)webView { m_activity.hidden= FALSE; [m_activity startAnimating]; NSLog(@"Web View Did finish loading"); }
Matt Long.. 19
为什么要在init中将活动指示器设置为nil?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { //Initialization code m_activity = nil; } return self; }
对super的调用从您的XIB初始化您的指示器(假设您将它连接到IB中的插座),但是在初始化之后您将引用设置为nil.删除该行.然后返回界面构建器并设置"隐藏时隐藏"复选框.现在,您可以简化显示指标的代码:
- (void)webViewDidFinishLoad:(UIWebView *)webView { [m_activity stopAnimating]; } - (void)webViewDidStartLoad:(UIWebView *)webView { [m_activity startAnimating]; }
"停止时隐藏"会导致指示器在停止动画时隐藏.
为什么要在init中将活动指示器设置为nil?
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { //Initialization code m_activity = nil; } return self; }
对super的调用从您的XIB初始化您的指示器(假设您将它连接到IB中的插座),但是在初始化之后您将引用设置为nil.删除该行.然后返回界面构建器并设置"隐藏时隐藏"复选框.现在,您可以简化显示指标的代码:
- (void)webViewDidFinishLoad:(UIWebView *)webView { [m_activity stopAnimating]; } - (void)webViewDidStartLoad:(UIWebView *)webView { [m_activity startAnimating]; }
"停止时隐藏"会导致指示器在停止动画时隐藏.