我正在使用xCode 4.3.2。在这个项目中,当我打电话给reloadXMLdata'按下按钮,它没有显示activityIndicator,在加载时看起来像是“挂了”,几秒钟后它就会填满数据。如何在加载时显示activityIndicator? 你可以帮我解决这个问题吗?
-(void)loadXML
{
NSString *urlAddress = [NSString stringWithFormat:@"my_xml_url"];
NSURL *url = [NSURL URLWithString:urlAddress];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlParser setDelegate:self];
BOOL success = [xmlParser parse];
if(success)
{
[self.activityIndicator setHidden:TRUE];
[self.activityIndicator stopAnimating];
[dataTable reloadData];
}
else
NSLog(@"Error!!!");
}
-(IBAction)reloadXMLdata:(id) sender
{
[self.activityIndicator setHidden:FALSE];
[self.activityIndicator startAnimating];
[self loadXML];
}
答案 0 :(得分:0)
问题是您在调用loadXML
时阻止了UI,并且活动指示器没有机会显示。因此,请尝试使用GCD,如下所示:
-(IBAction)reloadXMLdata:(id) sender
{
[self.activityIndicator setHidden:FALSE];
[self.activityIndicator startAnimating];
[self loadXML];
}
-(void)loadXML
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *urlAddress = [NSString stringWithFormat:@"my_xml_url"];
NSURL *url = [NSURL URLWithString:urlAddress];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlParser setDelegate:self];
BOOL success = [xmlParser parse];
if(success) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicator setHidden:TRUE];
[self.activityIndicator stopAnimating];
[dataTable reloadData];
});
} else {
NSLog(@"Error!!!");
}
});
}