使用UIActivityIndicatorView的活动指示器(微调器) [英] Activity indicator (spinner) with UIActivityIndicatorView
问题描述
我有一个tableView加载XML feed,如下:
I have a tableView that loads an XML feed as follows:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([stories count] == 0) {
NSString *path = @"http://myurl.com/file.xml";
[self parseXMLFileAtURL:path];
}
}
我想让微调器显示
I'd like to have the spinner to show on the top bar at the app launch and dissapear once the data is displayed on my tableView.
我认为将开头放在viewDidAppear,结束于 - (void)parserDidEndDocument:(NSXMLParser *)parser
但它不工作。
I thought that putting the beginning at viewDidAppear and the end at -(void)parserDidEndDocument:(NSXMLParser *)parser
but it didn't work.
如何实现此解决方案。
推荐答案
这里有问题: NSXMLParser
同步API。这意味着,一旦你在你的 NSXMLParser
上调用 parse
,那个线程将完全停滞解析xml,
Here's the problem: NSXMLParser
is a synchronous API. That means that as soon as you call parse
on your NSXMLParser
, that thread is going to be totally stuck parsing xml, which means no UI updates.
以下是我通常解决这个问题的方法:
Here's how I usually solve this:
- (void) startThingsUp {
//put the spinner onto the screen
//start the spinner animating
NSString *path = @"http://myurl.com/file.xml";
[self performSelectorInBackground:@selector(parseXMLFileAtURL:) withObject:path];
}
- (void) parseXMLFileAtURL:(NSString *)path {
//do stuff
[xmlParser parse];
[self performSelectorOnMainThread:@selector(doneParsing) withObject:nil waitUntilDone:NO];
}
- (void) doneParsing {
//stop the spinner
//remove it from the screen
}
我已经多次使用这个方法了,它的工作原理很好。
I've used this method many times, and it works beautifully.
这篇关于使用UIActivityIndicatorView的活动指示器(微调器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!