[英]xcode - Re call xml from server with “activityIndicator”
I'm using xCode 4.3.2. 我正在使用xCode 4.3.2。 In this project, When i call 'reloadXMLdata' with button click, its not showing the activityIndicator, by the time of loading it looks like 'hanged', after few seconds it filling the data. 在这个项目中,当我单击按钮调用“ reloadXMLdata”时,它在加载时没有显示activityIndicator,在加载时它看起来像是“已挂起”,几秒钟后便填充了数据。 How can i show activityIndicator by the time of loading? 如何在加载时显示activityIndicator? Could you please help me for fixing this? 您能帮我解决这个问题吗?
-(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];
}
The problem is that you block the UI when calling loadXML
and the activity indicator doesn't have the opportunity to be displayed. 问题是您在调用loadXML
时阻塞了UI,并且活动指示器没有机会显示。 So try using GCD like this: 因此,尝试像这样使用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!!!");
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.