我有一个加载 XML feed 的 tableView,如下所示:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([stories count] == 0) {
NSString *path = @"http://myurl.com/file.xml";
[self parseXMLFileAtURL:path];
}
}
我希望微调器在应用程序启动时显示在顶部栏上,并在数据显示在我的 tableView 上后消失。
我认为将开头放在 viewDidAppear 并将结尾放在-(void)parserDidEndDocument:(NSXMLParser *)parser
但它不起作用。
我希望有一个关于如何实施该解决方案的良好解释解决方案。
问题是这样的:NSXMLParser
是一个同步API。这意味着只要您致电parse
在你的NSXMLParser
,该线程将完全陷入解析 xml 的状态,这意味着没有 UI 更新。
我通常是这样解决这个问题的:
- (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
}
这个方法我已经用过很多次了,效果非常好。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)