我知道这是一个非常常见的问题,我阅读了stackoverflow中提出的大部分问题,但仍然无法弄清楚如何从视图中删除uiactivityindicatorview。 请在下面找到代码
@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];
}
头文件
@interface FeedBackViewController : UIViewController<UIWebViewDelegate>{
IBOutlet UIWebView *webView;
IBOutlet UIActivityIndicatorView *m_activity;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) UIActivityIndicatorView *m_activity;
另外,我已经检查过IB中的方框,说明在停止时隐藏。我将NSLog(@"in webViewDidFinishLoad")
放在webViewFinishLoad方法中,似乎由于某种原因它没有触及该方法。
和
NSLog(@"m_activity = %@",m_activity);
在viewDidLoad方法中,它在控制台中显示如下消息
> m_activity = <UIActivityIndicatorView:
> 0x4d3d920; frame = (150 79; 20 20);
> hidden = YES; opaque = NO; autoresize
> = RM+BM; layer = <CALayer: 0x4d35ce0>>
答案 0 :(得分:3)
您的webViewDidFinishLoad
函数可能未被调用。如果Web视图无法打开URL,则可能会发生这种情况。在任何情况下,您还应该实现webView:didFailLoadWithError:
方法并在那里停止活动指示器。
-edit -
要从UIWebView接收事件,您需要将控制器设置为UIWebView的委托。仅实现UIWebViewDelegate是不够的。您可以通过更新viewDidLoad
方法执行此操作,如下所示:
-(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"]]];
}
答案 1 :(得分:0)
您应该在代码中添加NSLog(@"in webViewDidFinishLoad")
行,并观察控制台以查看代码是否符合这些要点。由于这是一个非常简单的例子,也许m_activity没有连接到实际的对象?在将viewDidLoad添加到viewDidLoad的末尾时,尝试查看是否获得地址或null:
NSLog(@"m_activity = %@",m_activity);