我有一个包含 UIWebView 的视图控制器。我想在右上角显示一个活动指示器,以便用户知道网页正在加载;但是在构建和运行时,活动监视器不显示。
我有一个名为 webView 的 UIWebView 插座和一个名为 activityIndicator 的 UIActivityIndicatorView 插座。
这是我的实现:
- (void)viewDidLoad
{
[super viewDidLoad];
webView.delegate=self;
[self.view addSubview:activityIndicator];
NSString *fullURL = @"http://www.oncologyeducation.com"; NSURL *url = [NSURL URLWithString:fullURL]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
- (void)viewDidUnload
{
[self setWebView:nil];
[self setActivityIndicator:nil];
[self setActivityIndicator:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
return NO;
} else {
return YES;
}
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
return YES;
}
-(void) webViewDidStartLoad:(UIWebView *)webView{
NSLog(@"load started");
[activityIndicator startAnimating];
activityIndicator.hidden = NO;
}
-(void) webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"load finished");
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
}
-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
}
@end
在我的故事板上,我有一个连接到文件所有者的活动指示器视图。感谢您的任何建议!