Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

objective c - How implement a UIActivityIndicatorView when the UIWebView is Loading? (iPhone ObjC)

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");
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...