How do you start activity indicator?
I want to start an activity indicator whenever t开发者_Python百科he user presses any link on a webview.
First assign the ViewController which the UIWebView appears in as its delegate: Do this either by control-dragging from the UIWebView to the File's Owner object in InterfaceBuilder or in code:
[myWebView setDelegate:myViewController];
Then in your ViewController.m file you can now use the delegate methods to detect when pages are being loaded. These delegate methods will be triggered every time a link or new page is loaded in the UIWebView.
- (void)webViewDidStartLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
If you wanna show network activity indicator just put
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
and then
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
when you wanna hide it
while if you needs UIActivityIndicator
you have to define it in your controller then Outlet it in XIB and then
show
[activityIndicator startAnimating];
hide
[activityIndicator stopAnimating];
Take a look at UIWebViewDelegate Protocol.
It will allow your delegate to be notified whenever the webview is starting to load new content, which, presumably, is the case whenever a link has been pressed. The delegate method you want is webViewDidStartLoad: - in its implementation you could then easily set up or start your activity indicator.
Use the method webViewDidStartLoad:
in UIWebViewDelegate
.
精彩评论