How implement a UIActivityIndicatorView when the U

2019-01-25 10:14发布

I want to know how to implement an activityIndicator in a WebView based app, I wrote the following code but the indicator does not appear.

The webview load file locally, so it load very fast, but when it load an external page it load slow and I need the indicator...

FirstViewController.h

 #import <UIKit/UIKit.h>

 @interface FirstViewController : 
 UIViewController <UIWebViewDelegate>{
    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");
 }

3条回答
劫难
2楼-- · 2019-01-25 10:26

UIWebView.loading property can also be used.

Apple's doc: @property(nonatomic, readonly, getter=isLoading) BOOL loading Description A Boolean value indicating whether the receiver is done loading content. (read-only) If YES, the receiver is still loading content; otherwise, NO.

In iOS6 it looks like Apple has fixed some issues with this property as well. http://code-gotcha.blogspot.fi/2012/08/uiwebviewloading-in-ios-6-fixed.html

查看更多
聊天终结者
3楼-- · 2019-01-25 10:28

Whats the issue here, the code you posted above should work, except that you dont initialize the indicator anywhere (maybe you do in viewDidLoad) but the code shown above should work given that the indicator was initialized correctly and u set the webview d elegate to the view controller there, I have it working on some of my apps where i use webviews and indicators to indicate when its loading...

查看更多
你好瞎i
4楼-- · 2019-01-25 10:31

Why are you setting your activity indicator to nil in your init?

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     
  if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {      
     //Initialization code      
     m_activity = nil;  
   }    
   return self; 
}

The call to super initialized your indicator from your XIB (assuming you connected it to your outlet in IB), but then you are setting the reference to nil after it's been initialized. Remove that line. Then go back into interface builder and set the "Hide when stopped" checkbox. Now you can simplify your code that displays the indicator:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   [m_activity stopAnimating];  
}

- (void)webViewDidStartLoad:(UIWebView *)webView {     
   [m_activity startAnimating];     
}

The "Hide when stopped" causes the indicator to hide when you stop it from animating.

查看更多
登录 后发表回答