我在使用NSURLConnection请求帮助从服务器收集数据时显示UIActivityIndicatorView时出现问题。
我认为请求是异步的,即在新线程中启动。我复制了Apple的AdvancedTableViewCells示例。我在iOS 4.3 iPhone模拟器中的XCode中运行它。我还没有在真正的iPhone上测试它。
此外,我搜索了这个问题并尝试了很多建议,但感觉是我忘记了一些基本的东西。下面是我的RootViewController类的代码。
我只是选择一行,创建并添加activityview,startanimating,然后创建NSUrlConnection对象,该对象开始从另一个线程中的服务器获取数据,我相信。
有什么想法吗?
@interface RootViewController : UITableViewController {
NSMutableData *receivedData;
UIActivityIndicatorView *activityView;
}
@end
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// In my rootviewcontroller
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:activityView];
[activityView startAnimating];
…
NSMutableURLRequest *tUrlRequest = [tQuery createUrlRequest:tStatId];
NSURLConnection *tConnectionResponse = [[NSURLConnection alloc] initWithRequest: tUrlRequest delegate: self];
if (!tConnectionResponse) {
NSLog(@"Failed to submit request");
} else {
NSLog(@"Request submitted");
receivedData = [[NSMutableData data] retain];
}
return;
}
...
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
NSXMLParser *tParser = [[NSXMLParser alloc] initWithData: receivedData];
...
[tParser parse];
...
[connection release];
[receivedData release];
[NSThread sleepForTimeInterval: 2.0]; // Just to see if activity view will show up...
NSUInteger row = 1;
if (row != NSNotFound)
{
// Create the view controller and initialize it with the
// next level of data.
VivadataTViewController *vivaViewController = [[VivadataTViewController alloc] init];
if (activityView != nil) {
[activityView stopAnimating];
}
}
}
答案 0 :(得分:0)
如果出现同样的问题,请尝试更改属性检查器下的 UIActivityIndicatorView 的颜色 - > 样式到灰色