我第一次在 iPhone 应用程序中使用两个线程,但遇到了问题。在这个原型中,我有一个加载本地网页的视图控制器。我希望在页面加载完成之前显示一个活动指示器。使用下面的代码,活动指示器启动,页面加载正确,但活动指示器没有停止或隐藏。看起来从来没有调用过“加载”函数。
我究竟做错了什么?
- (void)viewDidLoad {
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];
[NSThread detachNewThreadSelector:@selector(getData) toTarget:self withObject:nil];
[super viewDidLoad];
}
- (void)getData {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[detailWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"page1" ofType:@"html"]isDirectory:NO]]];
[NSTimer scheduledTimerWithTimeInterval: (1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
[pool release];
}
- (void)loading {
if(!detailWebView.loading){
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
}