问题
i know this is a very common question, i read most of the questions asked in stackoverflow but still couldn't figure out how to remove the uiactivityindicatorview from the view. please find below the code
@implementation FeedBackViewController
@synthesize m_activity,webView;
-(void)viewDidLoad{
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://equinox.library.pitt.edu/limesurvey/index.php?sid=87435&lang=en"]]];
}
- (void)dealloc {
[m_activity release];
[webView release];
[super dealloc];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[m_activity stopAnimating];
[m_activity removeFromSuperview];
m_activity = nil;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"in webViewDidFinishLoad")
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
m_activity.center = self.view.center;
[self.view addSubview: m_activity];
[m_activity startAnimating];
}
the header file
@interface FeedBackViewController : UIViewController<UIWebViewDelegate>{
IBOutlet UIWebView *webView;
IBOutlet UIActivityIndicatorView *m_activity;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) UIActivityIndicatorView *m_activity;
Also, i have checked the box in the IB that says, hide when stopped. I put NSLog(@"in webViewDidFinishLoad")
in the webViewFinishLoad method, seems like it does not hit that method for some reason.
and for
NSLog(@"m_activity = %@",m_activity);
in the viewDidLoad method, it gives a message as below in the console
> m_activity = <UIActivityIndicatorView:
> 0x4d3d920; frame = (150 79; 20 20);
> hidden = YES; opaque = NO; autoresize
> = RM+BM; layer = <CALayer: 0x4d35ce0>>
回答1:
Your webViewDidFinishLoad
function is probably not being called. It may happen if the web view fails to open the url. In any case, you should also implement webView:didFailLoadWithError:
method and stop the activity indicator there too.
-edit-
To receive events from UIWebView you need to set the controller as the UIWebView's delegate. Just implementing the UIWebViewDelegate is not sufficient. You can do this by updating viewDidLoad
method as follows:
-(void)viewDidLoad{
[super viewDidLoad];
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://equinox.library.pitt.edu/limesurvey/index.php?sid=87435&lang=en"]]];
}
回答2:
You should add NSLog(@"in webViewDidFinishLoad")
lines to your code and watch the console to see if the code hits those points. Since this is a very simple example, perhaps m_activity is not connected to the actual object? Try to see if you get an address or a null when you add this to the end of your viewDidLoad:
NSLog(@"m_activity = %@",m_activity);
来源:https://stackoverflow.com/questions/4521214/iphone-uiactivityindicatorview-does-not-hide