UIActivityIndicatorView Not disappearing
This seems to be a very simplistic issue, but I literally cannot get it working.
I have a section of my app which takes my user to a WebView displaying my site. As part of the built in 'browser' experience i want a UIActivityIndicator located to let the user know that the site is loading. Obviously when this is done i want the indicator to disappear.
At the minute, the activity indicator animate开发者_JAVA百科s, but then keeps animating forever and does not stop or disappear.
The code I am using in .h is as follows;
@interface AppSupportWebsite : UIViewController <UIWebViewDelegate> {
IBOutlet UIActivityIndicatorView *activityIndicator;
}
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@end
And within my .m file I have the following; (abbreviated to just show the activity indicator specific code)
@synthesize activityIndicator;
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
}
I have created an activity indicator within Interface Builder and through Files Owner connected the activityIndicator IBOutlet to the white activity indicator through IB but it just wont disappear.
select ur activity indicator then
cmd+1
select "hide when stopped"
check ur webview.delegate=self;
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"finished");
[self.activityIndicator stopAnimating];
}
This is just a stab in the dark, but you may be having issues because the UIWebView Delegate is being called from a different thread other than the main thread. So you may be getting some race conditions as a result.
Give this Grand Central Dispatch Code (GCD) a try and let me know if that works:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
dispatch_async(dispatch_get_main_queue(), ^(void) {
[activityIndicator stopAnimating];
});
}
Can you add an NSLog statement (just something like NSLog(@"Web Finished Loading!");) to the webViewDidFinishLoad method? My guess is that this method is not being called. There could be a few reasons for that. For instance, if you are releasing the webView before you hit the method, it might not call the method at all.
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}
It is possible to run above code multiple time it your website have multiple redirects. so I would not suggest to put this line in this delegate method. I would suggest to put this in viewWillAppear delegate method of viewController.
-(void)viewWillAppear:(BOOL)animated{
[activityIndicator startAnimating];
}
Also, make sure you bind UIActivityIndicator in your xib.
Hope this help.
精彩评论